diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d3fee8c..867b906 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,4 +31,30 @@ jobs: uses: "ramsey/composer-install@v1" - name: "Run PHPUnit" - run: "vendor/bin/simple-phpunit --coverage-text" + run: "vendor/bin/phpunit --coverage-text" + + phpstan: + runs-on: "ubuntu-20.04" + + strategy: + fail-fast: false + matrix: + php-version: + - "7.4" + - "8.0" + - "8.1" + + steps: + - uses: actions/checkout@v2 + + - name: "Install PHP ${{ matrix.php-version }}" + uses: "shivammathur/setup-php@v2" + with: + php-version: "${{ matrix.php-version }}" + coverage: "pcov" + + - name: "Install dependencies with Composer" + uses: "ramsey/composer-install@v1" + + - name: "Run PHPStan" + run: "vendor/bin/phpstan analyse src --level 5" diff --git a/composer.json b/composer.json index a60bc88..56d3863 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,8 @@ } }, "require-dev": { - "symfony/phpunit-bridge": "^5.0" + "symfony/phpunit-bridge": "^5.0", + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^1.4" } } diff --git a/src/Negotiation/AbstractNegotiator.php b/src/Negotiation/AbstractNegotiator.php index 79c7c9b..3355e92 100644 --- a/src/Negotiation/AbstractNegotiator.php +++ b/src/Negotiation/AbstractNegotiator.php @@ -11,7 +11,7 @@ abstract class AbstractNegotiator * @param string $header A string containing an `Accept|Accept-*` header. * @param array $priorities A set of server priorities. * - * @return AcceptHeader|null best matching type + * @return BaseAccept|null best matching type */ public function getBest($header, array $priorities, $strict = false) { @@ -100,18 +100,18 @@ public function getOrderedElements($header) /** * @param string $header accept header part or server priority * - * @return AcceptHeader Parsed header object + * @return BaseAccept Parsed header object */ abstract protected function acceptFactory($header); /** - * @param AcceptHeader $header - * @param AcceptHeader $priority + * @param BaseAccept $header + * @param BaseAccept $priority * @param integer $index * * @return AcceptMatch|null Headers matched */ - protected function match(AcceptHeader $header, AcceptHeader $priority, $index) + protected function match(BaseAccept $header, BaseAccept $priority, $index) { $ac = $header->getType(); $pc = $priority->getType(); @@ -130,7 +130,7 @@ protected function match(AcceptHeader $header, AcceptHeader $priority, $index) /** * @param string $header A string that contains an `Accept*` header. * - * @return AcceptHeader[] + * @return string[] */ private function parseHeader($header) { @@ -144,8 +144,8 @@ private function parseHeader($header) } /** - * @param AcceptHeader[] $headerParts - * @param Priority[] $priorities Configured priorities + * @param BaseAccept[] $headerParts + * @param BaseAccept[] $priorities Configured priorities * * @return AcceptMatch[] Headers matched */ diff --git a/src/Negotiation/Accept.php b/src/Negotiation/Accept.php index 281ae27..27c1c88 100644 --- a/src/Negotiation/Accept.php +++ b/src/Negotiation/Accept.php @@ -4,7 +4,7 @@ use Negotiation\Exception\InvalidMediaType; -final class Accept extends BaseAccept implements AcceptHeader +final class Accept extends BaseAccept { private $basePart; diff --git a/src/Negotiation/AcceptCharset.php b/src/Negotiation/AcceptCharset.php index 7ce3490..1bd4e92 100644 --- a/src/Negotiation/AcceptCharset.php +++ b/src/Negotiation/AcceptCharset.php @@ -2,6 +2,6 @@ namespace Negotiation; -final class AcceptCharset extends BaseAccept implements AcceptHeader +final class AcceptCharset extends BaseAccept { } diff --git a/src/Negotiation/AcceptEncoding.php b/src/Negotiation/AcceptEncoding.php index 8165a7f..9ba89c7 100644 --- a/src/Negotiation/AcceptEncoding.php +++ b/src/Negotiation/AcceptEncoding.php @@ -2,6 +2,6 @@ namespace Negotiation; -final class AcceptEncoding extends BaseAccept implements AcceptHeader +final class AcceptEncoding extends BaseAccept { } diff --git a/src/Negotiation/AcceptLanguage.php b/src/Negotiation/AcceptLanguage.php index ad1c032..fed5991 100644 --- a/src/Negotiation/AcceptLanguage.php +++ b/src/Negotiation/AcceptLanguage.php @@ -4,7 +4,7 @@ use Negotiation\Exception\InvalidLanguage; -final class AcceptLanguage extends BaseAccept implements AcceptHeader +final class AcceptLanguage extends BaseAccept { private $language; private $script; @@ -32,7 +32,7 @@ public function __construct($value) } /** - * @return string + * @return null|string */ public function getSubPart() { @@ -46,4 +46,12 @@ public function getBasePart() { return $this->language; } + + /** + * @return null|string + */ + public function getScript() + { + return $this->script; + } } diff --git a/src/Negotiation/BaseAccept.php b/src/Negotiation/BaseAccept.php index a4663d2..8435000 100644 --- a/src/Negotiation/BaseAccept.php +++ b/src/Negotiation/BaseAccept.php @@ -2,7 +2,7 @@ namespace Negotiation; -abstract class BaseAccept +abstract class BaseAccept implements AcceptHeader { /** * @var float @@ -140,14 +140,13 @@ private function parseParameters($acceptPart) } /** - * @param string $parameters + * @param array $parameters * * @return string */ private function buildParametersString($parameters) { $parts = []; - ksort($parameters); foreach ($parameters as $key => $val) { $parts[] = sprintf('%s=%s', $key, $val); diff --git a/src/Negotiation/LanguageNegotiator.php b/src/Negotiation/LanguageNegotiator.php index ba33616..9e79c01 100644 --- a/src/Negotiation/LanguageNegotiator.php +++ b/src/Negotiation/LanguageNegotiator.php @@ -15,7 +15,7 @@ protected function acceptFactory($accept) /** * {@inheritdoc} */ - protected function match(AcceptHeader $acceptLanguage, AcceptHeader $priority, $index) + protected function match(BaseAccept $acceptLanguage, BaseAccept $priority, $index) { if (!$acceptLanguage instanceof AcceptLanguage || !$priority instanceof AcceptLanguage) { return null; diff --git a/tests/Negotiation/Tests/AcceptLanguageTest.php b/tests/Negotiation/Tests/AcceptLanguageTest.php index bc8ab24..ccd674e 100644 --- a/tests/Negotiation/Tests/AcceptLanguageTest.php +++ b/tests/Negotiation/Tests/AcceptLanguageTest.php @@ -42,6 +42,12 @@ public function testGetValue($header, $expected) } + public function testGetScript() + { + $accept = new AcceptLanguage("zh-Hans-CN;q=0.3"); + $this->assertSame("hans", $accept->getScript()); + } + public static function dataProviderForGetValue() { return array(