Skip to content

Commit

Permalink
Run PHPStan as Github Action
Browse files Browse the repository at this point in the history
* Depend on PHPUnit for development

In order to run the unit tests, PHPUnit is a hard dev
dependency, so I've included it in this commit, and now
I can run the unit tests as part of this PR.

* Depend on PHPStan for development

This is for willdurand#89 - to ensure correct type hints are provided
to developers who use IDEs.

* Fix object model of AcceptHeader interface

Fixes willdurand#89 - IDEs and PHPStan are happy with this implementation

* Correct return type

* Correct nonexistent Priority class to AcceptHeader

* Improve typehint - allow looser type to be returned

* Improve typehint - more accurate types as parameters

* Improve typehint - more accurate generics as parameters

* Expose script property - was only ever written

* Properly typehint associative array

* Typehint nullable string

* Match typehints of parent method

* Add PHPStan to CI

* Configure PHPUnit versions for different PHP runtimes

* Use real phpunit
  • Loading branch information
g105b authored Feb 15, 2022
1 parent 68e9ea0 commit c00bd21
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 19 deletions.
28 changes: 27 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
}
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0"
"symfony/phpunit-bridge": "^5.0",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.4"
}
}
16 changes: 8 additions & 8 deletions src/Negotiation/AbstractNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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)
{
Expand All @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/Accept.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Negotiation\Exception\InvalidMediaType;

final class Accept extends BaseAccept implements AcceptHeader
final class Accept extends BaseAccept
{
private $basePart;

Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/AcceptCharset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Negotiation;

final class AcceptCharset extends BaseAccept implements AcceptHeader
final class AcceptCharset extends BaseAccept
{
}
2 changes: 1 addition & 1 deletion src/Negotiation/AcceptEncoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Negotiation;

final class AcceptEncoding extends BaseAccept implements AcceptHeader
final class AcceptEncoding extends BaseAccept
{
}
12 changes: 10 additions & 2 deletions src/Negotiation/AcceptLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Negotiation\Exception\InvalidLanguage;

final class AcceptLanguage extends BaseAccept implements AcceptHeader
final class AcceptLanguage extends BaseAccept
{
private $language;
private $script;
Expand Down Expand Up @@ -32,7 +32,7 @@ public function __construct($value)
}

/**
* @return string
* @return null|string
*/
public function getSubPart()
{
Expand All @@ -46,4 +46,12 @@ public function getBasePart()
{
return $this->language;
}

/**
* @return null|string
*/
public function getScript()
{
return $this->script;
}
}
5 changes: 2 additions & 3 deletions src/Negotiation/BaseAccept.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Negotiation;

abstract class BaseAccept
abstract class BaseAccept implements AcceptHeader
{
/**
* @var float
Expand Down Expand Up @@ -140,14 +140,13 @@ private function parseParameters($acceptPart)
}

/**
* @param string $parameters
* @param array<string, string> $parameters
*
* @return string
*/
private function buildParametersString($parameters)
{
$parts = [];

ksort($parameters);
foreach ($parameters as $key => $val) {
$parts[] = sprintf('%s=%s', $key, $val);
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/LanguageNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/Negotiation/Tests/AcceptLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c00bd21

Please sign in to comment.