Skip to content

Commit

Permalink
Check is_file before filemtime
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-tech committed Sep 4, 2024
1 parent 50f9235 commit c8e4c44
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/Doctrine/Common/Annotations/CachedReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function array_merge;
use function assert;
use function filemtime;
use function is_file;
use function max;
use function time;

Expand Down Expand Up @@ -229,7 +230,7 @@ private function getLastModification(ReflectionClass $class): int
$parent = $class->getParentClass();

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

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

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

$lastModificationTime = max(array_merge(
[$fileName ? filemtime($fileName) : 0],
[is_file($fileName) ? filemtime($fileName) : 0],
array_map(function (ReflectionClass $reflectionTrait): int {
return $this->getTraitLastModificationTime($reflectionTrait);
}, $reflectionTrait->getTraits())
Expand Down
21 changes: 21 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/PsrCachedReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,27 @@ public function testReaderIsNotHitIfCacheIsFresh(): void
);
}

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

eval($code);

$readAnnotations = (new PsrCachedReader(new AnnotationReader(), new ArrayAdapter(), true))
->getClassAnnotations(new ReflectionClass(PsrEvalClass::class));

Check failure on line 239 in tests/Doctrine/Tests/Common/Annotations/PsrCachedReaderTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis / PHPStan (8.2)

Class Doctrine\Tests\Common\Annotations\PsrEvalClass not found.

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

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

0 comments on commit c8e4c44

Please sign in to comment.