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

#186 check if file exists before asking for mtime #436

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions lib/Doctrine/Common/Annotations/CachedReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use function array_map;
use function array_merge;
use function assert;
use function file_exists;
use function filemtime;
use function is_file;
use function max;
use function time;

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

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

$lastModificationTime = max(array_merge(
[$fileName ? filemtime($fileName) : 0],
[self::getFileMtime($fileName)],
array_map(function (ReflectionClass $reflectionTrait): int {
return $this->getTraitLastModificationTime($reflectionTrait);
}, $reflectionTrait->getTraits())
Expand All @@ -265,4 +267,9 @@ private function getTraitLastModificationTime(ReflectionClass $reflectionTrait):

return $this->loadedFilemtimes[$fileName] = $lastModificationTime;
}

private static function getFileMtime(string $fileName): int
{
return file_exists($fileName) && is_file($fileName) ? filemtime($fileName) : 0;
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to call both file_exists and is_file?

Copy link
Member

Choose a reason for hiding this comment

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

return file_exists($fileName) ? filemtime($fileName) : 0;

should do the trick too, shouldn't it?

Copy link

Choose a reason for hiding this comment

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

The edge case that is_file checks for here is if $fileName is a directory. I guess it makes sense to return 0 in this case, but I don't know if :

  1. It's the focus of this PR
  2. getFileMtime() can get called with a directory name at all

Copy link
Member

Choose a reason for hiding this comment

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

Isn't file_exists redundant if we have is_file?

Copy link

Choose a reason for hiding this comment

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

}
}
9 changes: 7 additions & 2 deletions lib/Doctrine/Common/Annotations/PsrCachedReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private function getLastModification(ReflectionClass $class): int
$parent = $class->getParentClass();

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

$lastModificationTime = max(array_merge(
[$fileName ? filemtime($fileName) : 0],
[self::getFileMtime($fileName)],
array_map(function (ReflectionClass $reflectionTrait): int {
return $this->getTraitLastModificationTime($reflectionTrait);
}, $reflectionTrait->getTraits())
Expand All @@ -229,4 +229,9 @@ private function getTraitLastModificationTime(ReflectionClass $reflectionTrait):

return $this->loadedFilemtimes[$fileName] = $lastModificationTime;
}

private static function getFileMtime(string $fileName): int
{
return file_exists($fileName) && is_file($fileName) ? filemtime($fileName) : 0;
}
}
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));

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

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