Skip to content

Commit ceca425

Browse files
Merge branch '9.2'
2 parents 1b32fa7 + 31ae9e7 commit ceca425

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

.php-cs-fixer.dist.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
'yield',
5555
],
5656
],
57-
'braces' => true,
57+
'braces' => [
58+
'position_after_anonymous_constructs' => 'next',
59+
],
5860
'cast_spaces' => true,
5961
'class_attributes_separation' => [
6062
'elements' => [

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1616

1717
## [9.2.12] - 2022-MM-DD
1818

19+
### Changed
20+
21+
* [#898](https://github.com/sebastianbergmann/php-code-coverage/pull/898): Use content hash instead of `filemtime()` to determine cache hit/miss
22+
1923
### Fixed
2024

2125
* [#736](https://github.com/sebastianbergmann/php-code-coverage/issues/736): HTML report generator allows invalid values for low upper bound and high lower bound

src/Node/File.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,8 @@ private function processFunctions(array $functions): void
502502
$this->functions[$functionName]['executedBranches'] = count(
503503
array_filter(
504504
$this->functionCoverageData[$functionName]['branches'],
505-
static function (array $branch) {
505+
static function (array $branch)
506+
{
506507
return (bool) $branch['hit'];
507508
}
508509
)
@@ -517,7 +518,8 @@ static function (array $branch) {
517518
$this->functions[$functionName]['executedPaths'] = count(
518519
array_filter(
519520
$this->functionCoverageData[$functionName]['paths'],
520-
static function (array $path) {
521+
static function (array $path)
522+
{
521523
return (bool) $path['hit'];
522524
}
523525
)
@@ -561,7 +563,8 @@ private function newMethod(string $className, string $methodName, array $method,
561563
$methodData['executedBranches'] = count(
562564
array_filter(
563565
$this->functionCoverageData[$key]['branches'],
564-
static function (array $branch) {
566+
static function (array $branch)
567+
{
565568
return (bool) $branch['hit'];
566569
}
567570
)
@@ -576,7 +579,8 @@ static function (array $branch) {
576579
$methodData['executedPaths'] = count(
577580
array_filter(
578581
$this->functionCoverageData[$key]['paths'],
579-
static function (array $path) {
582+
static function (array $path)
583+
{
580584
return (bool) $path['hit'];
581585
}
582586
)

src/StaticAnalysis/CachingFileAnalyser.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
1111

12+
use function crc32;
13+
use function file_get_contents;
14+
use function file_put_contents;
15+
use function is_file;
16+
use function serialize;
1217
use SebastianBergmann\CodeCoverage\Directory;
1318

1419
/**
@@ -91,8 +96,10 @@ public function ignoredLinesFor(string $filename): array
9196

9297
public function process(string $filename): void
9398
{
94-
if ($this->has($filename)) {
95-
$this->cache[$filename] = $this->read($filename);
99+
$cache = $this->read($filename);
100+
101+
if ($cache !== false) {
102+
$this->cache[$filename] = $cache;
96103

97104
return;
98105
}
@@ -109,26 +116,20 @@ public function process(string $filename): void
109116
$this->write($filename, $this->cache[$filename]);
110117
}
111118

112-
private function has(string $filename): bool
119+
/**
120+
* @return mixed
121+
*/
122+
private function read(string $filename): array|false
113123
{
114124
$cacheFile = $this->cacheFile($filename);
115125

116126
if (!is_file($cacheFile)) {
117127
return false;
118128
}
119129

120-
if (filemtime($cacheFile) < filemtime($filename)) {
121-
return false;
122-
}
123-
124-
return true;
125-
}
126-
127-
private function read(string $filename): array
128-
{
129130
return unserialize(
130131
file_get_contents(
131-
$this->cacheFile($filename)
132+
$cacheFile
132133
),
133134
['allowed_classes' => false]
134135
);
@@ -144,6 +145,6 @@ private function write(string $filename, array $data): void
144145

145146
private function cacheFile(string $filename): string
146147
{
147-
return $this->directory . DIRECTORY_SEPARATOR . hash('sha256', $filename . self::CACHE_FORMAT_VERSION);
148+
return $this->directory . DIRECTORY_SEPARATOR . hash('sha256', $filename . crc32(file_get_contents($filename)) . self::CACHE_FORMAT_VERSION);
148149
}
149150
}

0 commit comments

Comments
 (0)