Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type hint #99

Closed
wants to merge 12 commits into from
16 changes: 8 additions & 8 deletions src/Negotiation/AbstractNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ abstract class AbstractNegotiator
*
* @return AcceptHeader|null best matching type
*/
public function getBest($header, array $priorities, $strict = false)
public function getBest(string $header, array $priorities,bool $strict = false): ?AcceptHeader
{
if (empty($priorities)) {
if ($priorities === []) {
throw new InvalidArgument('A set of server priorities should be given.');
}

if (!$header) {
if ($header === '') {
throw new InvalidArgument('The header string should not be empty.');
}

Expand Down Expand Up @@ -55,9 +55,9 @@ public function getBest($header, array $priorities, $strict = false)
/**
* @param string $header A string containing an `Accept|Accept-*` header.
*
* @return [AcceptHeader] An ordered list of accept header elements
* @return AcceptHeader[] An ordered list of accept header elements
*/
public function getOrderedElements($header)
public function getOrderedElements(string $header): array
{
if (!$header) {
throw new InvalidArgument('The header string should not be empty.');
Expand Down Expand Up @@ -111,7 +111,7 @@ abstract protected function acceptFactory($header);
*
* @return Match|null Headers matched
*/
protected function match(AcceptHeader $header, AcceptHeader $priority, $index)
protected function match(AcceptHeader $header, AcceptHeader $priority, $index): ?Match
{
$ac = $header->getType();
$pc = $priority->getType();
Expand All @@ -132,7 +132,7 @@ protected function match(AcceptHeader $header, AcceptHeader $priority, $index)
*
* @return AcceptHeader[]
*/
private function parseHeader($header)
private function parseHeader($header): array
{
$res = preg_match_all('/(?:[^,"]*+(?:"[^"]*+")?)+[^,"]*+/', $header, $matches);

Expand All @@ -149,7 +149,7 @@ private function parseHeader($header)
*
* @return Match[] Headers matched
*/
private function findMatches(array $headerParts, array $priorities)
private function findMatches(array $headerParts, array $priorities): array
{
$matches = [];
foreach ($priorities as $index => $p) {
Expand Down
4 changes: 2 additions & 2 deletions src/Negotiation/Accept.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public function __construct($value)
/**
* @return string
*/
public function getSubPart()
public function getSubPart(): string
{
return $this->subPart;
}

/**
* @return string
*/
public function getBasePart()
public function getBasePart(): string
{
return $this->basePart;
}
Expand Down
20 changes: 10 additions & 10 deletions src/Negotiation/BaseAccept.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class BaseAccept
/**
* @param string $value
*/
public function __construct($value)
public function __construct(string $value)
{
list($type, $parameters) = $this->parseParameters($value);

Expand All @@ -52,39 +52,39 @@ public function __construct($value)
/**
* @return string
*/
public function getNormalizedValue()
public function getNormalizedValue(): string
{
return $this->normalized;
}

/**
* @return string
*/
public function getValue()
public function getValue(): string
{
return $this->value;
}

/**
* @return string
*/
public function getType()
public function getType(): string
{
return $this->type;
}

/**
* @return float
*/
public function getQuality()
public function getQuality(): float
{
return $this->quality;
}

/**
* @return array
*/
public function getParameters()
public function getParameters(): array
{
return $this->parameters;
}
Expand All @@ -95,7 +95,7 @@ public function getParameters()
*
* @return string|null
*/
public function getParameter($key, $default = null)
public function getParameter(string $key, $default = null): ?string
{
return isset($this->parameters[$key]) ? $this->parameters[$key] : $default;
}
Expand All @@ -105,7 +105,7 @@ public function getParameter($key, $default = null)
*
* @return boolean
*/
public function hasParameter($key)
public function hasParameter(string $key): bool
{
return isset($this->parameters[$key]);
}
Expand All @@ -115,7 +115,7 @@ public function hasParameter($key)
* @param string $acceptPart
* @return array
*/
private function parseParameters($acceptPart)
private function parseParameters(string $acceptPart): array
{
$parts = explode(';', $acceptPart);
$type = array_shift($parts);
Expand All @@ -140,7 +140,7 @@ private function parseParameters($acceptPart)
*
* @return string
*/
private function buildParametersString($parameters)
private function buildParametersString($parameters): string
{
$parts = [];

Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/CharsetNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CharsetNegotiator extends AbstractNegotiator
/**
* {@inheritdoc}
*/
protected function acceptFactory($accept)
protected function acceptFactory($accept): AcceptCharset
{
return new AcceptCharset($accept);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/EncodingNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EncodingNegotiator extends AbstractNegotiator
/**
* {@inheritdoc}
*/
protected function acceptFactory($accept)
protected function acceptFactory($accept): AcceptEncoding
{
return new AcceptEncoding($accept);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Negotiation/LanguageNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class LanguageNegotiator extends AbstractNegotiator
/**
* {@inheritdoc}
*/
protected function acceptFactory($accept)
protected function acceptFactory($accept): AccepLanguage
{
return new AcceptLanguage($accept);
}

/**
* {@inheritdoc}
*/
protected function match(AcceptHeader $acceptLanguage, AcceptHeader $priority, $index)
protected function match(AcceptHeader $acceptLanguage, AcceptHeader $priority, $index): ?Match
{
if (!$acceptLanguage instanceof AcceptLanguage || !$priority instanceof AcceptLanguage) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function compare(Match $a, Match $b)
*
* @return Match[]
*/
public static function reduce(array $carry, Match $match)
public static function reduce(array $carry, Match $match): array
{
if (!isset($carry[$match->index]) || $carry[$match->index]->score < $match->score) {
$carry[$match->index] = $match;
Expand Down
6 changes: 3 additions & 3 deletions src/Negotiation/Negotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class Negotiator extends AbstractNegotiator
/**
* {@inheritdoc}
*/
protected function acceptFactory($accept)
protected function acceptFactory($accept): Accept
{
return new Accept($accept);
}

/**
* {@inheritdoc}
*/
protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
protected function match(AcceptHeader $accept, AcceptHeader $priority, $index): ?Match
{
if (!$accept instanceof Accept || !$priority instanceof Accept) {
return null;
Expand Down Expand Up @@ -78,7 +78,7 @@ protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
* should allow wildcards for either the portion before the "+" or
* after. This method splits the subpart to allow such matching.
*/
protected function splitSubPart($subPart)
protected function splitSubPart($subPart): array
{
if (!strstr($subPart, '+')) {
return [$subPart, ''];
Expand Down