Skip to content

Commit

Permalink
allow numeric-strings beeing returned for non-empty-string
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 16, 2022
1 parent 2b0ba44 commit 6696f45
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
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()) {
return TrinaryLogic::createYes();
}

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

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

public function testBug7265(): void
{
$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;
}
}

0 comments on commit 6696f45

Please sign in to comment.