Skip to content

Commit 253dca4

Browse files
Fix filemtime() warning because of eval'd code (#494)
* Check is_file before filemtime * Fix PHPStan class not found error * Fix PHPStan error "class not found" * Add check for filename is false * Change class name in two eval'ed snippets * Use an array cache instead of a mock --------- Co-authored-by: Alexander M. Turek <me@derrabus.de>
1 parent b25dcbb commit 253dca4

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

lib/Doctrine/Common/Annotations/CachedReader.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function array_merge;
1212
use function assert;
1313
use function filemtime;
14+
use function is_file;
1415
use function max;
1516
use function time;
1617

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

231232
$lastModification = max(array_merge(
232-
[$filename ? filemtime($filename) : 0],
233+
[$filename !== false && is_file($filename) ? filemtime($filename) : 0],
233234
array_map(function (ReflectionClass $reflectionTrait): int {
234235
return $this->getTraitLastModificationTime($reflectionTrait);
235236
}, $class->getTraits()),
@@ -253,7 +254,7 @@ private function getTraitLastModificationTime(ReflectionClass $reflectionTrait):
253254
}
254255

255256
$lastModificationTime = max(array_merge(
256-
[$fileName ? filemtime($fileName) : 0],
257+
[$fileName !== false && is_file($fileName) ? filemtime($fileName) : 0],
257258
array_map(function (ReflectionClass $reflectionTrait): int {
258259
return $this->getTraitLastModificationTime($reflectionTrait);
259260
}, $reflectionTrait->getTraits())

lib/Doctrine/Common/Annotations/PsrCachedReader.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function array_merge;
1313
use function assert;
1414
use function filemtime;
15+
use function is_file;
1516
use function max;
1617
use function rawurlencode;
1718
use function time;
@@ -195,7 +196,7 @@ private function getLastModification(ReflectionClass $class): int
195196
$parent = $class->getParentClass();
196197

197198
$lastModification = max(array_merge(
198-
[$filename ? filemtime($filename) : 0],
199+
[$filename !== false && is_file($filename) ? filemtime($filename) : 0],
199200
array_map(function (ReflectionClass $reflectionTrait): int {
200201
return $this->getTraitLastModificationTime($reflectionTrait);
201202
}, $class->getTraits()),
@@ -219,7 +220,7 @@ private function getTraitLastModificationTime(ReflectionClass $reflectionTrait):
219220
}
220221

221222
$lastModificationTime = max(array_merge(
222-
[$fileName ? filemtime($fileName) : 0],
223+
[$fileName !== false && is_file($fileName) ? filemtime($fileName) : 0],
223224
array_map(function (ReflectionClass $reflectionTrait): int {
224225
return $this->getTraitLastModificationTime($reflectionTrait);
225226
}, $reflectionTrait->getTraits())

tests/Doctrine/Tests/Common/Annotations/CachedReaderTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Doctrine\Common\Annotations\Reader;
88
use Doctrine\Common\Cache\ArrayCache;
99
use Doctrine\Common\Cache\Cache;
10+
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
1011
use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Route;
1112
use Doctrine\Tests\Common\Annotations\Fixtures\ClassThatUsesTraitThatUsesAnotherTraitWithMethods;
1213
use PHPUnit\Framework\MockObject\MockObject;
1314
use ReflectionClass;
1415
use ReflectionMethod;
16+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1517

1618
use function assert;
1719
use function class_exists;
@@ -225,6 +227,38 @@ public function testAvoidCallingFilemtimeTooMuch(): void
225227
$this->assertEquals([$route2], $reader->getMethodAnnotations(new ReflectionMethod($className, 'method2')));
226228
}
227229

230+
/**
231+
* @group 62
232+
*/
233+
public function testReaderDoesNotCacheIfFileDoesNotExistSoLastModificationCannotBeDetermined(): void
234+
{
235+
$code = <<<'EOS'
236+
namespace Doctrine\Tests\Common\Annotations;
237+
238+
/**
239+
* @\Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetClass("Some data")
240+
*/
241+
class CachedEvalClass {
242+
243+
}
244+
EOS;
245+
246+
eval($code);
247+
248+
if (class_exists(ArrayCache::class)) {
249+
$cache = new ArrayCache();
250+
} else {
251+
$cache = DoctrineProvider::wrap(new ArrayAdapter());
252+
}
253+
254+
$reader = new CachedReader(new AnnotationReader(), $cache, true);
255+
// @phpstan-ignore class.notFound
256+
$readAnnotations = $reader->getClassAnnotations(new ReflectionClass(CachedEvalClass::class));
257+
258+
self::assertIsArray($readAnnotations);
259+
self::assertCount(1, $readAnnotations);
260+
}
261+
228262
protected function doTestCacheStale(string $className, int $lastCacheModification): CachedReader
229263
{
230264
$cacheKey = $className;

tests/Doctrine/Tests/Common/Annotations/PsrCachedReaderTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,28 @@ public function testReaderIsNotHitIfCacheIsFresh(): void
220220
);
221221
}
222222

223+
public function testReaderDoesNotCacheIfFileDoesNotExistSoLastModificationCannotBeDetermined(): void
224+
{
225+
$code = <<<'EOS'
226+
namespace Doctrine\Tests\Common\Annotations;
227+
228+
/**
229+
* @\Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetClass("Some data")
230+
*/
231+
class PsrCachedEvalClass {
232+
233+
}
234+
EOS;
235+
236+
eval($code);
237+
238+
$reader = new PsrCachedReader(new AnnotationReader(), new ArrayAdapter(), true);
239+
// @phpstan-ignore class.notFound
240+
$readAnnotations = $reader->getClassAnnotations(new ReflectionClass(PsrCachedEvalClass::class));
241+
242+
self::assertCount(1, $readAnnotations);
243+
}
244+
223245
protected function doTestCacheStale(string $className, int $lastCacheModification): PsrCachedReader
224246
{
225247
$cacheKey = rawurlencode($className);

0 commit comments

Comments
 (0)