diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 4b4510d..df2db46 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -18,7 +18,7 @@ tests/* - + diff --git a/composer.json b/composer.json index 2979148..f62c52d 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^1.0", "phpcompatibility/php-compatibility": "^9.3", - "phpstan/phpstan": "^1.10", + "phpstan/phpstan": "^2.1", "squizlabs/php_codesniffer": "^3.7", "symfony/phpunit-bridge": "^5.2 || ^6.2 || ^7.0" }, diff --git a/phpstan.neon.dist b/phpstan.neon.dist index b980ecc..3cbe92e 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,7 +1,15 @@ parameters: - level: 6 + level: 10 + + # Code to be analyzed paths: - src - tests + + # Don't worry about the coverage reports excludePaths: - - tests/coverage + - tests/coverage (?) + + # PHPUnit Bridge puts the PHPUnit source in an unconventional location + scanDirectories: + - vendor/bin/.phpunit/phpunit diff --git a/src/MarkupAssertionsTrait.php b/src/MarkupAssertionsTrait.php index c580bc8..ea94aea 100644 --- a/src/MarkupAssertionsTrait.php +++ b/src/MarkupAssertionsTrait.php @@ -127,11 +127,7 @@ public function assertNotHasElementWithAttributes($attributes = [], $markup = '' */ public function assertElementContains($contents, $selector = '', $markup = '', $message = '') { - $method = method_exists($this, 'assertStringContainsString') - ? 'assertStringContainsString' - : 'assertContains'; // @codeCoverageIgnore - - $this->$method( + $this->assertStringContainsString( $contents, $this->getInnerHtmlOfMatchedElements($markup, $selector), $message @@ -152,11 +148,7 @@ public function assertElementContains($contents, $selector = '', $markup = '', $ */ public function assertElementNotContains($contents, $selector = '', $markup = '', $message = '') { - $method = method_exists($this, 'assertStringNotContainsString') - ? 'assertStringNotContainsString' - : 'assertNotContains'; // @codeCoverageIgnore - - $this->$method( + $this->assertStringNotContainsString( $contents, $this->getInnerHtmlOfMatchedElements($markup, $selector), $message @@ -177,6 +169,7 @@ public function assertElementNotContains($contents, $selector = '', $markup = '' */ public function assertElementRegExp($regexp, $selector = '', $markup = '', $message = '') { + // @phpstan-ignore function.alreadyNarrowedType (Introduced in PHPUnit 9.x, PHP 7.3+) $method = method_exists($this, 'assertMatchesRegularExpression') ? 'assertMatchesRegularExpression' : 'assertRegExp'; // @codeCoverageIgnore @@ -202,6 +195,7 @@ public function assertElementRegExp($regexp, $selector = '', $markup = '', $mess */ public function assertElementNotRegExp($regexp, $selector = '', $markup = '', $message = '') { + // @phpstan-ignore function.alreadyNarrowedType (Introduced in PHPUnit 9.x, PHP 7.3+) $method = method_exists($this, 'assertDoesNotMatchRegularExpression') ? 'assertDoesNotMatchRegularExpression' : 'assertNotRegExp'; // @codeCoverageIgnore @@ -252,7 +246,7 @@ private function flattenAttributeArray(array $attributes) if (empty($value)) { $value = sprintf('[%s]', $key); } else { - $value = sprintf('[%s="%s"]', $key, htmlspecialchars($value)); + $value = sprintf('[%s="%s"]', $key, htmlspecialchars((string) $value)); } }); @@ -276,10 +270,14 @@ private function getInnerHtmlOfMatchedElements($markup, $query) // Loop through results and collect their innerHTML values. foreach ($results as $result) { + if (!isset($result->firstChild)) { + continue; + } + $document = new \DOMDocument(); $document->appendChild($document->importNode($result->firstChild, true)); - $contents[] = trim(html_entity_decode($document->saveHTML())); + $contents[] = trim(html_entity_decode((string) $document->saveHTML())); } return implode(PHP_EOL, $contents); diff --git a/tests/MarkupAssertionsTraitTest.php b/tests/MarkupAssertionsTraitTest.php index 642927d..2666c8f 100644 --- a/tests/MarkupAssertionsTraitTest.php +++ b/tests/MarkupAssertionsTraitTest.php @@ -285,7 +285,7 @@ public function getInnerHtmlOfMatchedElements_should_retrieve_the_inner_HTML( /** * Data provider for testFlattenAttributeArray(). * - * @return array,string}> + * @return array,string}> */ public function provideAttributes(): array {