Skip to content

Commit

Permalink
Fix analysis with newer PHPStan
Browse files Browse the repository at this point in the history
This was prompted by upgrading PHPStan from 1.10.62 to 1.12.5:

- Remove redundant checks thanks to better `preg_match_all` typing.

- Assert that `preg_match` matched successfully before using the `$match` result in a test (it would be an empty array when the match failed).

- Replace deprecated `checkMissingIterableValueType` by `ignoreErrors` with an identifier.
    > ⚠️  You're using a deprecated config option checkMissingIterableValueType ⚠️️
    >
    > It's strongly recommended to remove it from your configuration file
    > and add the missing array typehints.
    >
    > If you want to continue ignoring missing typehints from arrays,
    > add missingType.iterableValue error identifier to your ignoreErrors:
    >
    > parameters:
    >	ignoreErrors:
    >		-
    >			identifier: missingType.iterableValue

- Bump minimum PHPStan version since we now rely on identifiers.

- Remove deprecated `checkGenericClassInNonGenericObjectType`, it does not look like it is actually needed any more.
    > ⚠️  You're using a deprecated config option checkGenericClassInNonGenericObjectType ⚠️️
    >
    > It's strongly recommended to remove it from your configuration file
    > and add the missing generic typehints.
    >
    > If you want to continue ignoring missing typehints from generics,
    > add missingType.generics error identifier to your ignoreErrors:
    >
    > parameters:
    >	ignoreErrors:
    >		-
    >			identifier: missingType.generics

- Assert for more precise `substr` return type on PHP < 8.0.
  • Loading branch information
jtojnar committed Sep 28, 2024
1 parent 4c04c40 commit 6677119
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"php-http/guzzle7-adapter": "^1.0",
"php-http/mock-client": "^1.4",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^1.2",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"rector/rector": "^0.12.11",
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ parameters:
-
message: '#expects DOMElement, DOMNode given#'
path: %currentWorkingDirectory%/src/Graby.php
-
identifier: missingType.iterableValue

inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
2 changes: 1 addition & 1 deletion src/Extractor/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function fetch(UriInterface $url, bool $skipTypeVerification = false, arr
// (regex inspired from here: https://stackoverflow.com/a/55083809/954513)
preg_match_all('/<!--(?:\[| ?<!).+?-->/mis', $body, $matchesConditional);

if (isset($matchesConditional[0]) && (is_countable($matchesConditional[0]) ? \count($matchesConditional[0]) : 0) > 1) {
if (\count($matchesConditional[0]) > 1) {
foreach ($matchesConditional as $conditionalComment) {
$body = str_replace($conditionalComment, '', $body);
}
Expand Down
6 changes: 6 additions & 0 deletions src/SiteConfig/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function addToCache(string $key, SiteConfig $config): void
$key = strtolower($key);
if (str_starts_with($key, 'www.')) {
$key = substr($key, 4);
// For PHPStan on PHP < 8.0: it cannot fail since the prefix checked above has four characters.
\assert(false !== $key);
}

if ($config->cache_key) {
Expand All @@ -90,6 +92,8 @@ public function getCachedVersion(string $key): ?SiteConfig
$key = strtolower($key);
if (str_starts_with($key, 'www.')) {
$key = substr($key, 4);
// For PHPStan on PHP < 8.0: it cannot fail since the prefix checked above has four characters.
\assert(false !== $key);
}

if (\array_key_exists($key, $this->cache)) {
Expand Down Expand Up @@ -127,6 +131,8 @@ public function buildForHost(string $host, bool $addToCache = true): SiteConfig
$host = strtolower($host);
if (str_starts_with($host, 'www.')) {
$host = substr($host, 4);
// For PHPStan on PHP < 8.0: it cannot fail since the prefix checked above has four characters.
\assert(false !== $host);
}

// is merged version already cached?
Expand Down
3 changes: 2 additions & 1 deletion tests/GrabyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function dataForFetchContent(): array

$test = (string) file_get_contents($file->getRealpath());

preg_match('/-----URL-----\s*(.*?)\s*-----URL_EFFECTIVE-----\s*(.*?)\s*-----HEADER-----\s*(.*?)\s*-----LANGUAGE-----\s*(.*?)\s*-----AUTHOR-----\s*(.*?)\s*-----TITLE-----\s*(.*?)\s*-----SUMMARY-----\s*(.*?)\s*-----RAW_CONTENT-----\s*(.*?)\s*(------RAW_CONTENT2-----\s*(.*?)\s*)?----PARSED_CONTENT-----\s*(.*)\s*/sx', $test, $match);
$parses = preg_match('/-----URL-----\s*(.*?)\s*-----URL_EFFECTIVE-----\s*(.*?)\s*-----HEADER-----\s*(.*?)\s*-----LANGUAGE-----\s*(.*?)\s*-----AUTHOR-----\s*(.*?)\s*-----TITLE-----\s*(.*?)\s*-----SUMMARY-----\s*(.*?)\s*-----RAW_CONTENT-----\s*(.*?)\s*(------RAW_CONTENT2-----\s*(.*?)\s*)?----PARSED_CONTENT-----\s*(.*)\s*/sx', $test, $match);
\assert(1 === $parses, \sprintf('File %s does not match the required pattern', $file->getRealpath()));

$tests[] = [
$match[1], // url
Expand Down

0 comments on commit 6677119

Please sign in to comment.