From 397aa8b567e03ce16e6e2ed28ad3b401a61382e2 Mon Sep 17 00:00:00 2001 From: Damien Debin Date: Thu, 6 Jan 2022 18:25:15 +0100 Subject: [PATCH] Adds compatibility with PHP 8.0 | 8.1. Fix some PHPStan warnings. --- .github/workflows/main.yaml | 18 +++++++++++------- composer.json | 8 ++++---- src/AbstractElement.php | 9 ++++----- src/Feed.php | 11 ++++++----- tests/FeedTest.php | 2 +- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index efb16e8..12c5d09 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -11,16 +11,17 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.1', '7.2', '7.3', '7.4'] + php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] name: PHP ${{ matrix.php }} tests steps: - uses: actions/checkout@v2 # required for "git tag" presence for MonorepoBuilder split and ChangelogLinker git tags resolver; default is 1 - run: git fetch --depth=100000 origin # see https://github.com/shivammathur/setup-php - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} + extensions: xml coverage: none - run: composer install --no-progress - run: composer phpunit @@ -31,9 +32,10 @@ jobs: - uses: actions/checkout@v2 - run: git fetch --depth=100000 origin # see https://github.com/shivammathur/setup-php - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: 7.1 + extensions: xml coverage: none - run: composer update --no-progress --prefer-lowest - run: composer phpunit @@ -44,9 +46,10 @@ jobs: - uses: actions/checkout@v2 - run: git fetch --depth=100000 origin # see https://github.com/shivammathur/setup-php - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: 7.1 + extensions: xml, xdebug coverage: xdebug - run: composer install --no-progress - run : | @@ -61,9 +64,10 @@ jobs: steps: - uses: actions/checkout@v2 # see https://github.com/shivammathur/setup-php - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: php-version: 7.4 + extensions: xml coverage: none - run: composer install --no-progress - run: composer php-cs-fixer-dry-run @@ -72,9 +76,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - uses: shivammathur/setup-php@v1 + - uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: 7.1 coverage: none - run: composer install --no-progress - run: composer phpstan \ No newline at end of file diff --git a/composer.json b/composer.json index 2a3357a..d0883fb 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ } ], "require": { - "php": "^7.1", + "php": "^7.1 || ^8.0", "ext-dom": "*", "ext-filter": "*", "ext-libxml": "*", @@ -25,9 +25,9 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", - "phpstan/phpstan": "^0.12.26", - "phpstan/phpstan-strict-rules": "^0.12", - "phpstan/phpstan-webmozart-assert": "^0.12", + "phpstan/phpstan": "^1.0", + "phpstan/phpstan-strict-rules": "^1.0", + "phpstan/phpstan-webmozart-assert": "^1.0", "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" }, "config": { diff --git a/src/AbstractElement.php b/src/AbstractElement.php index d492937..91f07e3 100644 --- a/src/AbstractElement.php +++ b/src/AbstractElement.php @@ -4,7 +4,6 @@ use DateTime; use DateTimeInterface; -use DOMElement; use InvalidArgumentException; use SimpleXMLElement; use Webmozart\Assert\Assert; @@ -38,7 +37,7 @@ abstract class AbstractElement /** @var string[][] */ protected $categories = []; - /** @var mixed[][] */ + /** @var string[][] */ protected $links = []; /** @@ -123,7 +122,7 @@ public function addLink(string $uri, ?string $rel = null, ?string $type = null, } if (null !== $length) { - $link['length'] = $length; + $link['length'] = (string) $length; } $this->links[] = $link; @@ -177,7 +176,7 @@ public function addChildrenTo(SimpleXMLElement $parent): void } /** - * @return mixed[] + * @return string[] */ protected static function createPerson(string $name, ?string $email = null, ?string $uri = null): array { @@ -220,7 +219,7 @@ protected static function addChildWithTypeToElement(SimpleXMLElement $parent, st protected static function addCData(string $cdataText, SimpleXMLElement $element): void { $node = dom_import_simplexml($element); - Assert::isInstanceOf($node, DOMElement::class); + assert(false !== $node); $no = $node->ownerDocument; assert(null !== $no); $node->appendChild($no->createCDATASection($cdataText)); diff --git a/src/Feed.php b/src/Feed.php index c530bee..2d0d31d 100644 --- a/src/Feed.php +++ b/src/Feed.php @@ -3,7 +3,6 @@ namespace AtomGenerator; use DOMDocument; -use DOMElement; use LibXMLError; use SimpleXMLElement; use Webmozart\Assert\Assert; @@ -37,7 +36,10 @@ class Feed extends AbstractElement /** @var null|string */ protected $generatorUri; - /** @var mixed[][] */ + /** + * @var mixed[][] + * @phpstan-var array + */ protected $customElements = []; /** @@ -114,10 +116,9 @@ public function getEntries(): array } /** - * @param mixed $value * @param null|string[] $attributes */ - public function addCustomElement(string $ns, string $uri, string $name, $value, ?array $attributes = null): void + public function addCustomElement(string $ns, string $uri, string $name, string $value, ?array $attributes = null): void { self::assertURL($uri); @@ -193,7 +194,7 @@ public function getSimpleXML(): SimpleXMLElement public function getDocument(): DOMDocument { $node = dom_import_simplexml($this->getSimpleXML()); - Assert::isInstanceOf($node, DOMElement::class); + assert(false !== $node); $no = $node->ownerDocument; assert(null !== $no); diff --git a/tests/FeedTest.php b/tests/FeedTest.php index 6cd066a..09c4ca8 100644 --- a/tests/FeedTest.php +++ b/tests/FeedTest.php @@ -40,7 +40,7 @@ public function testFeedCreation1(): void $feed->addContributor('contributor', 'contributor@test.com', 'http://test.com/contributor?a=b&c=d'); $feed->setUpdatedDateTime(new DateTime('2019-05-04T20:00:40Z')); $feed->addCustomElement('sy', 'http://purl.org/rss/1.0/modules/syndication', 'updatePeriod', 'hourly'); - $feed->addCustomElement('sy', 'http://purl.org/rss/1.0/modules/syndication', 'updateFrequency', 10); + $feed->addCustomElement('sy', 'http://purl.org/rss/1.0/modules/syndication', 'updateFrequency', '10'); $entry = new Entry(); $entry->setTitle('entry title', 'text');