Skip to content

Commit ba3ec8d

Browse files
Eliminate code duplication
1 parent a075b4d commit ba3ec8d

File tree

3 files changed

+93
-135
lines changed

3 files changed

+93
-135
lines changed

src/StaticAnalysis/Cache.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
11+
12+
use const DIRECTORY_SEPARATOR;
13+
use function file_exists;
14+
use function file_get_contents;
15+
use function file_put_contents;
16+
use function filemtime;
17+
use function hash;
18+
use function serialize;
19+
use function unserialize;
20+
use SebastianBergmann\CodeCoverage\Directory;
21+
22+
abstract class Cache
23+
{
24+
/**
25+
* @var string
26+
*/
27+
private $directory;
28+
29+
public function __construct(string $directory)
30+
{
31+
Directory::create($directory);
32+
33+
$this->directory = $directory;
34+
}
35+
36+
protected function cacheHas(string $filename, string $method): bool
37+
{
38+
$cacheFile = $this->cacheFile($filename, $method);
39+
40+
if (!file_exists($cacheFile)) {
41+
return false;
42+
}
43+
44+
if (filemtime($cacheFile) < filemtime($filename)) {
45+
return false;
46+
}
47+
48+
return true;
49+
}
50+
51+
/**
52+
* @psalm-param list<class-string> $allowedClasses
53+
*
54+
* @return mixed
55+
*/
56+
protected function cacheRead(string $filename, string $method, array $allowedClasses = [])
57+
{
58+
$options = ['allowed_classes' => false];
59+
60+
if (!empty($allowedClasses)) {
61+
$options = ['allowed_classes' => $allowedClasses];
62+
}
63+
64+
return unserialize(
65+
file_get_contents(
66+
$this->cacheFile($filename, $method)
67+
),
68+
[
69+
'allowed_classes' => $allowedClasses,
70+
]
71+
);
72+
}
73+
74+
/**
75+
* @param mixed $data
76+
*/
77+
protected function cacheWrite(string $filename, string $method, $data): void
78+
{
79+
file_put_contents(
80+
$this->cacheFile($filename, $method),
81+
serialize($data)
82+
);
83+
}
84+
85+
protected function cacheFile(string $filename, string $method): string
86+
{
87+
return $this->directory . DIRECTORY_SEPARATOR . hash('sha256', $filename . $method);
88+
}
89+
}

src/StaticAnalysis/CachingCoveredFileAnalyser.php

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,19 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
1111

12-
use const DIRECTORY_SEPARATOR;
13-
use function file_exists;
14-
use function file_get_contents;
15-
use function file_put_contents;
16-
use function filemtime;
17-
use function hash;
18-
use function serialize;
19-
use function unserialize;
20-
use SebastianBergmann\CodeCoverage\Directory;
2112
use SebastianBergmann\LinesOfCode\LinesOfCode;
2213

23-
final class CachingCoveredFileAnalyser implements CoveredFileAnalyser
14+
final class CachingCoveredFileAnalyser extends Cache implements CoveredFileAnalyser
2415
{
25-
/**
26-
* @var string
27-
*/
28-
private $directory;
29-
3016
/**
3117
* @var CoveredFileAnalyser
3218
*/
3319
private $coveredFileAnalyser;
3420

3521
public function __construct(string $directory, CoveredFileAnalyser $coveredFileAnalyser)
3622
{
37-
Directory::create($directory);
23+
parent::__construct($directory);
3824

39-
$this->directory = $directory;
4025
$this->coveredFileAnalyser = $coveredFileAnalyser;
4126
}
4227

@@ -104,58 +89,4 @@ public function ignoredLinesFor(string $filename): array
10489

10590
return $data;
10691
}
107-
108-
private function cacheFile(string $filename, string $method): string
109-
{
110-
return $this->directory . DIRECTORY_SEPARATOR . hash('sha256', $filename . $method);
111-
}
112-
113-
private function cacheHas(string $filename, string $method): bool
114-
{
115-
$cacheFile = $this->cacheFile($filename, $method);
116-
117-
if (!file_exists($cacheFile)) {
118-
return false;
119-
}
120-
121-
if (filemtime($cacheFile) < filemtime($filename)) {
122-
return false;
123-
}
124-
125-
return true;
126-
}
127-
128-
/**
129-
* @psalm-param list<string> $allowedClasses
130-
*
131-
* @return mixed
132-
*/
133-
private function cacheRead(string $filename, string $method, array $allowedClasses = [])
134-
{
135-
$options = ['allowed_classes' => false];
136-
137-
if (!empty($allowedClasses)) {
138-
$options = ['allowed_classes' => $allowedClasses];
139-
}
140-
141-
return unserialize(
142-
file_get_contents(
143-
$this->cacheFile($filename, $method)
144-
),
145-
[
146-
'allowed_classes' => $allowedClasses,
147-
]
148-
);
149-
}
150-
151-
/**
152-
* @param mixed $data
153-
*/
154-
private function cacheWrite(string $filename, string $method, $data): void
155-
{
156-
file_put_contents(
157-
$this->cacheFile($filename, $method),
158-
serialize($data)
159-
);
160-
}
16192
}

src/StaticAnalysis/CachingUncoveredFileAnalyser.php

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,17 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
1111

12-
use SebastianBergmann\CodeCoverage\Directory;
13-
14-
final class CachingUncoveredFileAnalyser implements UncoveredFileAnalyser
12+
final class CachingUncoveredFileAnalyser extends Cache implements UncoveredFileAnalyser
1513
{
16-
/**
17-
* @var string
18-
*/
19-
private $directory;
20-
2114
/**
2215
* @var UncoveredFileAnalyser
2316
*/
2417
private $uncoveredFileAnalyser;
2518

2619
public function __construct(string $directory, UncoveredFileAnalyser $uncoveredFileAnalyser)
2720
{
28-
Directory::create($directory);
21+
parent::__construct($directory);
2922

30-
$this->directory = $directory;
3123
$this->uncoveredFileAnalyser = $uncoveredFileAnalyser;
3224
}
3325

@@ -43,58 +35,4 @@ public function executableLinesIn(string $filename): array
4335

4436
return $data;
4537
}
46-
47-
private function cacheFile(string $filename, string $method): string
48-
{
49-
return $this->directory . DIRECTORY_SEPARATOR . hash('sha256', $filename . $method);
50-
}
51-
52-
private function cacheHas(string $filename, string $method): bool
53-
{
54-
$cacheFile = $this->cacheFile($filename, $method);
55-
56-
if (!file_exists($cacheFile)) {
57-
return false;
58-
}
59-
60-
if (filemtime($cacheFile) < filemtime($filename)) {
61-
return false;
62-
}
63-
64-
return true;
65-
}
66-
67-
/**
68-
* @psalm-param list<string> $allowedClasses
69-
*
70-
* @return mixed
71-
*/
72-
private function cacheRead(string $filename, string $method, array $allowedClasses = [])
73-
{
74-
$options = ['allowed_classes' => false];
75-
76-
if (!empty($allowedClasses)) {
77-
$options = ['allowed_classes' => $allowedClasses];
78-
}
79-
80-
return unserialize(
81-
file_get_contents(
82-
$this->cacheFile($filename, $method)
83-
),
84-
[
85-
'allowed_classes' => $allowedClasses,
86-
]
87-
);
88-
}
89-
90-
/**
91-
* @param mixed $data
92-
*/
93-
private function cacheWrite(string $filename, string $method, $data): void
94-
{
95-
file_put_contents(
96-
$this->cacheFile($filename, $method),
97-
serialize($data)
98-
);
99-
}
10038
}

0 commit comments

Comments
 (0)