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

Add support for PHP 8 #106

Merged
merged 5 commits into from Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/vendor/
composer.lock
.phpunit.result.cache
24 changes: 13 additions & 11 deletions .travis.yml
@@ -1,19 +1,21 @@
sudo: false
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3
- hhvm

matrix:
allow_failures:
- php: hhvm
include:
- php: 7.1
env: SYMFONY_PHPUNIT_VERSION=7.5
- php: 7.2
env: SYMFONY_PHPUNIT_VERSION=8.5
- php: 7.3
env: SYMFONY_PHPUNIT_VERSION=9.3
- php: 7.4
env: SYMFONY_PHPUNIT_VERSION=9.3
- php: nightly
env: SYMFONY_PHPUNIT_VERSION=9.3

before_script:
- composer self-update
- composer self-update --2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does --2 mean?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Force update composer to 2.0.0-RC1.

- composer install --prefer-dist --no-interaction

script: vendor/bin/phpunit --coverage-text
script: vendor/bin/simple-phpunit --coverage-text
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=7.0.0"
"php": ">=7.1.0"
},
"autoload": {
"psr-4": { "Negotiation\\": "src/Negotiation" }
Expand All @@ -25,6 +25,6 @@
}
},
"require-dev": {
"phpunit/phpunit": "^6.5"
"symfony/phpunit-bridge": "^5.0"
}
}
12 changes: 6 additions & 6 deletions phpunit.xml.dist
@@ -1,23 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Negotiation Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<coverage>
<include>
<directory>./src/Negotiation/</directory>
</whitelist>
</filter>
</include>
</coverage>
</phpunit>
12 changes: 6 additions & 6 deletions src/Negotiation/AbstractNegotiator.php
Expand Up @@ -43,9 +43,9 @@ public function getBest($header, array $priorities, $strict = false)
$acceptedPriorities[] = $this->acceptFactory($p);
}
$matches = $this->findMatches($acceptedHeaders, $acceptedPriorities);
$specificMatches = array_reduce($matches, 'Negotiation\Match::reduce', []);
$specificMatches = array_reduce($matches, 'Negotiation\AcceptMatch::reduce', []);

usort($specificMatches, 'Negotiation\Match::compare');
usort($specificMatches, 'Negotiation\AcceptMatch::compare');

$match = array_shift($specificMatches);

Expand Down Expand Up @@ -83,7 +83,7 @@ public function getOrderedElements($header)
$qB = $b[0];

if ($qA == $qB) {
return $a[1] > $b[1];
return $a[1] <=> $b[1];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a very useful change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just changes the return value from a boolean (which then gets converted to an int) to the proper expected return value (E.G -1, 0 or 1)

}

return ($qA > $qB) ? -1 : 1;
Expand All @@ -109,7 +109,7 @@ abstract protected function acceptFactory($header);
* @param AcceptHeader $priority
* @param integer $index
*
* @return Match|null Headers matched
* @return AcceptMatch|null Headers matched
pierredup marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function match(AcceptHeader $header, AcceptHeader $priority, $index)
{
Expand All @@ -121,7 +121,7 @@ protected function match(AcceptHeader $header, AcceptHeader $priority, $index)
if ($equal || $ac === '*') {
$score = 1 * $equal;

return new Match($header->getQuality() * $priority->getQuality(), $score, $index);
return new AcceptMatch($header->getQuality() * $priority->getQuality(), $score, $index);
}

return null;
Expand All @@ -147,7 +147,7 @@ private function parseHeader($header)
* @param AcceptHeader[] $headerParts
* @param Priority[] $priorities Configured priorities
*
* @return Match[] Headers matched
* @return AcceptMatch[] Headers matched
*/
private function findMatches(array $headerParts, array $priorities)
{
Expand Down
16 changes: 8 additions & 8 deletions src/Negotiation/Match.php → src/Negotiation/AcceptMatch.php
Expand Up @@ -2,7 +2,7 @@

namespace Negotiation;

final class Match
final class AcceptMatch
{
/**
* @var float
Expand All @@ -27,12 +27,12 @@ public function __construct($quality, $score, $index)
}

/**
* @param Match $a
* @param Match $b
* @param AcceptMatch $a
* @param AcceptMatch $b
*
* @return int
*/
public static function compare(Match $a, Match $b)
public static function compare(AcceptMatch $a, AcceptMatch $b)
{
if ($a->quality !== $b->quality) {
return $a->quality > $b->quality ? -1 : 1;
Expand All @@ -46,12 +46,12 @@ public static function compare(Match $a, Match $b)
}

/**
* @param array $carry reduced array
* @param Match $match match to be reduced
* @param array $carry reduced array
* @param AcceptMatch $match match to be reduced
*
* @return Match[]
* @return AcceptMatch[]
*/
public static function reduce(array $carry, Match $match)
public static function reduce(array $carry, AcceptMatch $match)
{
if (!isset($carry[$match->index]) || $carry[$match->index]->score < $match->score) {
$carry[$match->index] = $match;
Expand Down
2 changes: 1 addition & 1 deletion src/Negotiation/LanguageNegotiator.php
Expand Up @@ -33,7 +33,7 @@ protected function match(AcceptHeader $acceptLanguage, AcceptHeader $priority, $
if (($ab == '*' || $baseEqual) && ($as === null || $subEqual)) {
$score = 10 * $baseEqual + $subEqual;

return new Match($acceptLanguage->getQuality() * $priority->getQuality(), $score, $index);
return new AcceptMatch($acceptLanguage->getQuality() * $priority->getQuality(), $score, $index);
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Negotiation/Negotiator.php
Expand Up @@ -38,7 +38,7 @@ protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
) {
$score = 100 * $baseEqual + 10 * $subEqual + count($intersection);

return new Match($accept->getQuality() * $priority->getQuality(), $score, $index);
return new AcceptMatch($accept->getQuality() * $priority->getQuality(), $score, $index);
}

if (!strstr($acceptSub, '+') || !strstr($prioritySub, '+')) {
Expand All @@ -65,7 +65,7 @@ protected function match(AcceptHeader $accept, AcceptHeader $priority, $index)
) {
$score = 100 * $baseEqual + 10 * $subEqual + $plusEqual + count($intersection);

return new Match($accept->getQuality() * $priority->getQuality(), $score, $index);
return new AcceptMatch($accept->getQuality() * $priority->getQuality(), $score, $index);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion tests/Negotiation/Tests/CharsetNegotiatorTest.php
Expand Up @@ -12,7 +12,7 @@ class CharsetNegotiatorTest extends TestCase
*/
private $negotiator;

protected function setUp()
protected function setUp(): void
{
$this->negotiator = new CharsetNegotiator();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Negotiation/Tests/EncodingNegotiatorTest.php
Expand Up @@ -12,7 +12,7 @@ class EncodingNegotiatorTest extends TestCase
*/
private $negotiator;

protected function setUp()
protected function setUp(): void
{
$this->negotiator = new EncodingNegotiator();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Negotiation/Tests/LanguageNegotiatorTest.php
Expand Up @@ -13,7 +13,7 @@ class LanguageNegotiatorTest extends TestCase
*/
private $negotiator;

protected function setUp()
protected function setUp(): void
{
$this->negotiator = new LanguageNegotiator();
}
Expand Down
34 changes: 17 additions & 17 deletions tests/Negotiation/Tests/MatchTest.php
Expand Up @@ -2,7 +2,7 @@

namespace Negotiation\Tests;

use Negotiation\Match;
use Negotiation\AcceptMatch;

class MatchTest extends TestCase
{
Expand All @@ -11,17 +11,17 @@ class MatchTest extends TestCase
*/
public function testCompare($match1, $match2, $expected)
{
$this->assertEquals($expected, Match::compare($match1, $match2));
$this->assertEquals($expected, AcceptMatch::compare($match1, $match2));
}

public static function dataProviderForTestCompare()
{
return array(
array(new Match(1.0, 110, 1), new Match(1.0, 111, 1), 0),
array(new Match(0.1, 10, 1), new Match(0.1, 10, 2), -1),
array(new Match(0.5, 110, 5), new Match(0.5, 11, 4), 1),
array(new Match(0.4, 110, 1), new Match(0.6, 111, 3), 1),
array(new Match(0.6, 110, 1), new Match(0.4, 111, 3), -1),
array(new AcceptMatch(1.0, 110, 1), new AcceptMatch(1.0, 111, 1), 0),
array(new AcceptMatch(0.1, 10, 1), new AcceptMatch(0.1, 10, 2), -1),
array(new AcceptMatch(0.5, 110, 5), new AcceptMatch(0.5, 11, 4), 1),
array(new AcceptMatch(0.4, 110, 1), new AcceptMatch(0.6, 111, 3), 1),
array(new AcceptMatch(0.6, 110, 1), new AcceptMatch(0.4, 111, 3), -1),
);
}

Expand All @@ -30,26 +30,26 @@ public static function dataProviderForTestCompare()
*/
public function testReduce($carry, $match, $expected)
{
$this->assertEquals($expected, Match::reduce($carry, $match));
$this->assertEquals($expected, AcceptMatch::reduce($carry, $match));
}

public static function dataProviderForTestReduce()
{
return array(
array(
array(1 => new Match(1.0, 10, 1)),
new Match(0.5, 111, 1),
array(1 => new Match(0.5, 111, 1)),
array(1 => new AcceptMatch(1.0, 10, 1)),
new AcceptMatch(0.5, 111, 1),
array(1 => new AcceptMatch(0.5, 111, 1)),
),
array(
array(1 => new Match(1.0, 110, 1)),
new Match(0.5, 11, 1),
array(1 => new Match(1.0, 110, 1)),
array(1 => new AcceptMatch(1.0, 110, 1)),
new AcceptMatch(0.5, 11, 1),
array(1 => new AcceptMatch(1.0, 110, 1)),
),
array(
array(0 => new Match(1.0, 10, 1)),
new Match(0.5, 111, 1),
array(0 => new Match(1.0, 10, 1), 1 => new Match(0.5, 111, 1)),
array(0 => new AcceptMatch(1.0, 10, 1)),
new AcceptMatch(0.5, 111, 1),
array(0 => new AcceptMatch(1.0, 10, 1), 1 => new AcceptMatch(0.5, 111, 1)),
),
);
}
Expand Down
52 changes: 25 additions & 27 deletions tests/Negotiation/Tests/NegotiatorTest.php
Expand Up @@ -6,7 +6,7 @@
use Negotiation\Exception\InvalidMediaType;
use Negotiation\Negotiator;
use Negotiation\Accept;
use Negotiation\Match;
use Negotiation\AcceptMatch;

class NegotiatorTest extends TestCase
{
Expand All @@ -16,7 +16,7 @@ class NegotiatorTest extends TestCase
*/
private $negotiator;

protected function setUp()
protected function setUp(): void
{
$this->negotiator = new Negotiator();
}
Expand Down Expand Up @@ -160,11 +160,9 @@ public function testGetBestRespectsQualityOfSource()
$this->assertEquals('text/plain', $accept->getType());
}

/**
* @expectedException Negotiation\Exception\InvalidMediaType
*/
public function testGetBestInvalidMediaType()
{
$this->expectException(\Negotiation\Exception\InvalidMediaType::class);
$header = 'sdlfkj20ff; wdf';
$priorities = array('foo/qwer');

Expand Down Expand Up @@ -214,40 +212,40 @@ public static function dataProviderForTestFindMatches()
array(new Accept('text/html; charset=UTF-8'), new Accept('image/png; foo=bar; q=0.7'), new Accept('*/*; foo=bar; q=0.4')),
array(new Accept('text/html; charset=UTF-8'), new Accept('image/png; foo=bar'), new Accept('application/pdf')),
array(
new Match(1.0, 111, 0),
new Match(0.7, 111, 1),
new Match(0.4, 1, 1),
new AcceptMatch(1.0, 111, 0),
new AcceptMatch(0.7, 111, 1),
new AcceptMatch(0.4, 1, 1),
)
),
array(
array(new Accept('text/html'), new Accept('image/*; q=0.7')),
array(new Accept('text/html; asfd=qwer'), new Accept('image/png'), new Accept('application/pdf')),
array(
new Match(1.0, 110, 0),
new Match(0.7, 100, 1),
new AcceptMatch(1.0, 110, 0),
new AcceptMatch(0.7, 100, 1),
)
),
array( # https://tools.ietf.org/html/rfc7231#section-5.3.2
array(new Accept('text/*; q=0.3'), new Accept('text/html; q=0.7'), new Accept('text/html; level=1'), new Accept('text/html; level=2; q=0.4'), new Accept('*/*; q=0.5')),
array(new Accept('text/html; level=1'), new Accept('text/html'), new Accept('text/plain'), new Accept('image/jpeg'), new Accept('text/html; level=2'), new Accept('text/html; level=3')),
array(
new Match(0.3, 100, 0),
new Match(0.7, 110, 0),
new Match(1.0, 111, 0),
new Match(0.5, 0, 0),
new Match(0.3, 100, 1),
new Match(0.7, 110, 1),
new Match(0.5, 0, 1),
new Match(0.3, 100, 2),
new Match(0.5, 0, 2),
new Match(0.5, 0, 3),
new Match(0.3, 100, 4),
new Match(0.7, 110, 4),
new Match(0.4, 111, 4),
new Match(0.5, 0, 4),
new Match(0.3, 100, 5),
new Match(0.7, 110, 5),
new Match(0.5, 0, 5),
new AcceptMatch(0.3, 100, 0),
new AcceptMatch(0.7, 110, 0),
new AcceptMatch(1.0, 111, 0),
new AcceptMatch(0.5, 0, 0),
new AcceptMatch(0.3, 100, 1),
new AcceptMatch(0.7, 110, 1),
new AcceptMatch(0.5, 0, 1),
new AcceptMatch(0.3, 100, 2),
new AcceptMatch(0.5, 0, 2),
new AcceptMatch(0.5, 0, 3),
new AcceptMatch(0.3, 100, 4),
new AcceptMatch(0.7, 110, 4),
new AcceptMatch(0.4, 111, 4),
new AcceptMatch(0.5, 0, 4),
new AcceptMatch(0.3, 100, 5),
new AcceptMatch(0.7, 110, 5),
new AcceptMatch(0.5, 0, 5),
)
)
);
Expand Down