Skip to content

Commit

Permalink
Add check for filename is false
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-tech committed Sep 5, 2024
1 parent 2aa5323 commit 17922f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Annotations/CachedReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private function getLastModification(ReflectionClass $class): int
$parent = $class->getParentClass();

$lastModification = max(array_merge(
[is_file($filename) ? filemtime($filename) : 0],
[$filename !== false && is_file($filename) ? filemtime($filename) : 0],
array_map(function (ReflectionClass $reflectionTrait): int {
return $this->getTraitLastModificationTime($reflectionTrait);
}, $class->getTraits()),
Expand All @@ -254,7 +254,7 @@ private function getTraitLastModificationTime(ReflectionClass $reflectionTrait):
}

$lastModificationTime = max(array_merge(
[is_file($fileName) ? filemtime($fileName) : 0],
[$fileName !== false && is_file($fileName) ? filemtime($fileName) : 0],
array_map(function (ReflectionClass $reflectionTrait): int {
return $this->getTraitLastModificationTime($reflectionTrait);
}, $reflectionTrait->getTraits())
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Annotations/PsrCachedReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private function getLastModification(ReflectionClass $class): int
$parent = $class->getParentClass();

$lastModification = max(array_merge(
[is_file($filename) ? filemtime($filename) : 0],
[$filename !== false && is_file($filename) ? filemtime($filename) : 0],
array_map(function (ReflectionClass $reflectionTrait): int {
return $this->getTraitLastModificationTime($reflectionTrait);
}, $class->getTraits()),
Expand All @@ -220,7 +220,7 @@ private function getTraitLastModificationTime(ReflectionClass $reflectionTrait):
}

$lastModificationTime = max(array_merge(
[is_file($fileName) ? filemtime($fileName) : 0],
[$fileName !== false && is_file($fileName) ? filemtime($fileName) : 0],
array_map(function (ReflectionClass $reflectionTrait): int {
return $this->getTraitLastModificationTime($reflectionTrait);
}, $reflectionTrait->getTraits())
Expand Down
28 changes: 28 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/CachedReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@ public function testAvoidCallingFilemtimeTooMuch(): void
$this->assertEquals([$route2], $reader->getMethodAnnotations(new ReflectionMethod($className, 'method2')));
}

/**
* @group 62
*/
public function testReaderDoesNotCacheIfFileDoesNotExistSoLastModificationCannotBeDetermined(): void
{
$code = <<<'EOS'
namespace Doctrine\Tests\Common\Annotations;
/**
* @\Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetClass("Some data")
*/
class PsrEvalClass {
}
EOS;

eval($code);

$cache = $this->createMock('Doctrine\Common\Cache\Cache');
assert($cache instanceof Cache && $cache instanceof MockObject);

$reader = new CachedReader(new AnnotationReader(), $cache, true);
// @phpstan-ignore class.notFound
$readAnnotations = $reader->getClassAnnotations(new ReflectionClass(PsrEvalClass::class));

self::assertCount(1, $readAnnotations);
}

protected function doTestCacheStale(string $className, int $lastCacheModification): CachedReader
{
$cacheKey = $className;
Expand Down

0 comments on commit 17922f8

Please sign in to comment.