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

allow numeric-strings beeing returned for non-empty-string #1428

Merged
merged 2 commits into from Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Expand Up @@ -78,6 +78,10 @@ public function isSubTypeOf(Type $otherType): TrinaryLogic

public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic
{
if ($acceptingType->isNonEmptyString()->yes()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried adding this case in isSubTypeOf() instead, but this made other tests fail. therefore the fix looks like it is right now.

return TrinaryLogic::createYes();
}

return $this->isSubTypeOf($acceptingType);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Expand Up @@ -695,4 +695,10 @@ public function testConditionalTypes(): void
]);
}

public function testBug7265(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-7265.php'], []);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-7265.php
@@ -0,0 +1,25 @@
<?php

namespace Bug7265;

class HelloWorld
{
/**
* @param literal-string $assetPath e.g. css/cmsfasttrack.css or js/fasttrack.js
* @return non-empty-string
*/
static public function assetId(string $assetPath)
{
if (strpos($assetPath, 'css/') !== 0 && strpos($assetPath, 'js/') !== 0 && strpos($assetPath, 'img/') !== 0) {
throw new \RuntimeException('Invalid asset path "' . $assetPath . '"');
}

$mtime = filemtime($assetPath);

if ($mtime === false) {
throw new \RuntimeException('Unable to get filemtime for asset path "' . $assetPath . '"');
}

return (string)$mtime;
}
}