Skip to content

Commit 8969d3b

Browse files
authored
Merge pull request #3 from okapi-web/develop
Develop
2 parents d29c599 + 964fe58 commit 8969d3b

30 files changed

+577
-677
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ij_php_align_assignments = true
77
ij_php_align_class_constants = true
88
ij_php_align_enum_cases = true
99
ij_php_align_key_value_pairs = true
10-
ij_php_align_multiline_parameters_in_calls = true
10+
ij_php_align_multiline_parameters_in_calls = false
1111
ij_php_align_phpdoc_comments = true
1212
ij_php_align_phpdoc_param_names = true
1313
ij_php_blank_lines_before_package = 0

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ composer.lock
88
# PHPUnit
99
tests/coverage/
1010
tests/cache/
11+
tests/tmp/
1112
.phpunit.result.cache

composer.json

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "okapi/code-transformer",
33
"description": "PHP Code Transformer is a PHP library that allows you to modify and transform the source code of a loaded PHP class.",
4-
"version": "1.2.0",
4+
"version": "1.3.0",
55
"type": "library",
66
"homepage": "https://github.com/okapi-web/php-code-transformer",
77
"license": "MIT",
@@ -33,7 +33,7 @@
3333
"roave/better-reflection": "^6.8"
3434
},
3535
"require-dev": {
36-
"phpunit/phpunit": "dev-main",
36+
"phpunit/phpunit": "^10",
3737
"symfony/var-dumper": "^6.2"
3838
},
3939
"autoload": {
@@ -48,11 +48,5 @@
4848
"psr-4": {
4949
"Okapi\\CodeTransformer\\Tests\\": "tests/"
5050
}
51-
},
52-
"repositories": [
53-
{
54-
"type": "vcs",
55-
"url": "https://github.com/WalterWoshid/phpunit"
56-
}
57-
]
51+
}
5852
}

src/Core/AutoloadInterceptor/ClassLoader.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function loadClass($namespacedClass): bool
8080
/**
8181
* Find the path to the file and match and apply the transformers.
8282
*
83-
* @param $namespacedClass
83+
* @param class-string $namespacedClass
8484
*
8585
* @return false|string
8686
*
@@ -105,7 +105,6 @@ public function findFile($namespacedClass): false|string
105105
$filePath = Path::resolve($filePath);
106106

107107

108-
109108
// Query cache state
110109
$cacheState = $this->cacheStateManager->queryCacheState($filePath);
111110

@@ -145,11 +144,11 @@ protected function isInternal(string $namespacedClass): bool
145144
'Okapi\\CodeTransformer\\',
146145
'Okapi\\Path\\',
147146
'Okapi\\Wildcards\\',
148-
'DI\\'
147+
'DI\\',
149148
],
150149
[
151-
'Okapi\\CodeTransformer\\Tests\\'
152-
]
150+
'Okapi\\CodeTransformer\\Tests\\',
151+
],
153152
);
154153
}
155154
}

src/Core/Cache/CacheState.php

+33-160
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
abstract class CacheState
1111
{
1212
public const TYPE = 'type';
13+
public const DATA = 'data';
1314

1415
public const ORIGINAL_FILE_PATH_KEY = 'originalFilePath';
16+
public const NAMESPACED_CLASS_KEY = 'namespacedClass';
1517
public const MODIFICATION_TIME_KEY = 'modificationTime';
1618

1719
public string $originalFilePath;
18-
public int $modificationTime;
20+
protected string $namespacedClass;
21+
protected int $modificationTime;
1922

2023
/**
2124
* CacheState constructor.
@@ -80,18 +83,39 @@ public function getRequiredKeys(): array
8083
{
8184
return [
8285
static::ORIGINAL_FILE_PATH_KEY,
86+
static::NAMESPACED_CLASS_KEY,
8387
static::MODIFICATION_TIME_KEY,
8488
];
8589
}
8690

91+
/**
92+
* Create a cache state if it is valid.
93+
*
94+
* @param array $cacheStateArray
95+
*
96+
* @return self|null
97+
*/
98+
public function createIfValid(array $cacheStateArray): ?CacheState
99+
{
100+
if (!$this->valid($cacheStateArray)) {
101+
// @codeCoverageIgnoreStart
102+
return null;
103+
// @codeCoverageIgnoreEnd
104+
}
105+
106+
$this->setData($cacheStateArray);
107+
108+
return $this;
109+
}
110+
87111
/**
88112
* Validate the cache state.
89113
*
90114
* @param array<string, (string|int|string[])> $cacheStateArray
91115
*
92116
* @return bool
93117
*/
94-
public function valid(array $cacheStateArray): bool
118+
private function valid(array $cacheStateArray): bool
95119
{
96120
// Check if all required keys are present
97121
foreach ($this->getRequiredKeys() as $requiredKey) {
@@ -110,7 +134,7 @@ public function valid(array $cacheStateArray): bool
110134
*
111135
* @param array<string, (string|int|string[])> $cacheStateArray
112136
*/
113-
public function setData(array $cacheStateArray): void
137+
private function setData(array $cacheStateArray): void
114138
{
115139
foreach ($cacheStateArray as $key => $value) {
116140
$this->{$key} = $value;
@@ -124,6 +148,12 @@ public function setData(array $cacheStateArray): void
124148
*/
125149
public function isFresh(): bool
126150
{
151+
if (!file_exists($this->originalFilePath)) {
152+
// @codeCoverageIgnoreStart
153+
return false;
154+
// @codeCoverageIgnoreEnd
155+
}
156+
127157
if (filemtime($this->originalFilePath) > $this->modificationTime) {
128158
return false;
129159
}
@@ -137,161 +167,4 @@ public function isFresh(): bool
137167
* @return string|null
138168
*/
139169
abstract public function getFilePath(): ?string;
140-
141-
// /**
142-
// * CacheState constructor.
143-
// *
144-
// * @param string $originalFilePath
145-
// * @param string $className
146-
// * @param string|null $cachedFilePath
147-
// * @param int|null $transformedTime
148-
// * @param string[]|null $transformerFilePaths
149-
// */
150-
// public function __construct(
151-
// public string $originalFilePath,
152-
// public string $className,
153-
// public ?string $cachedFilePath,
154-
// public ?int $transformedTime,
155-
// public ?array $transformerFilePaths,
156-
// ) {}
157-
//
158-
// /**
159-
// * Use the cached file path if aspects have been applied.
160-
// * Otherwise, use the original file path if no aspects have been applied.
161-
// *
162-
// * @return string
163-
// */
164-
// public function getFilePath(): string
165-
// {
166-
// return $this->cachedFilePath ?? $this->originalFilePath;
167-
// }
168-
//
169-
//
170-
//
171-
//
172-
// /**
173-
// * Get the cache state as an array.
174-
// *
175-
// * @return array
176-
// */
177-
// public function toArray(): array
178-
// {
179-
// return [
180-
// $this->originalFilePath,
181-
// $this->className,
182-
// $this->cachedFilePath,
183-
// $this->transformedTime,
184-
// $this->transformerFilePaths,
185-
// ];
186-
// }
187-
//
188-
// /**
189-
// * Check if the cache is not outdated.
190-
// *
191-
// * @return bool
192-
// */
193-
// public function isFresh(): bool
194-
// {
195-
// // @codeCoverageIgnoreStart
196-
// // This should only happen if the project is misconfigured
197-
// if ($this->checkInfiniteLoop()) {
198-
// return false;
199-
// }
200-
// // @codeCoverageIgnoreEnd
201-
//
202-
// $allFiles = array_merge(
203-
// [$this->originalFilePath],
204-
// $this->transformerFilePaths,
205-
// );
206-
//
207-
// if ($this->checkFilesModified($allFiles)) {
208-
// return false;
209-
// }
210-
//
211-
// if ($this->cachedFilePath) {
212-
// $allFiles[] = $this->cachedFilePath;
213-
// }
214-
//
215-
// if (!$this->checkFilesExist($allFiles)) {
216-
// return false;
217-
// }
218-
//
219-
// if (!$this->checkTransformerCount()) {
220-
// return false;
221-
// }
222-
//
223-
// return true;
224-
// }
225-
//
226-
// /**
227-
// * Check if the cache is in an infinite loop.
228-
// *
229-
// * @return bool True if the cache is in an infinite loop
230-
// */
231-
// protected function checkInfiniteLoop(): bool
232-
// {
233-
// if ($this->cachedFilePath !== null) {
234-
// // Same original file and cached file
235-
// if ($this->originalFilePath === $this->cachedFilePath) {
236-
// return true;
237-
// }
238-
// }
239-
//
240-
// return false;
241-
// }
242-
//
243-
// /**
244-
// * Check if the files have been modified.
245-
// *
246-
// * @param string[] $files
247-
// *
248-
// * @return bool True if any file has been modified
249-
// */
250-
// protected function checkFilesModified(array $files): bool
251-
// {
252-
// $lastModified = max(array_map('filemtime', $files));
253-
// if ($lastModified >= $this->transformedTime) {
254-
// return true;
255-
// }
256-
//
257-
// return false;
258-
// }
259-
//
260-
// /**
261-
// * Check if the files exist.
262-
// *
263-
// * @param string[] $files
264-
// *
265-
// * @return bool True if all files exist
266-
// */
267-
// protected function checkFilesExist(array $files): bool
268-
// {
269-
// // Check if the cache file exists
270-
// foreach ($files as $file) {
271-
// if (!file_exists($file)) {
272-
// return false;
273-
// }
274-
// }
275-
//
276-
// return true;
277-
// }
278-
//
279-
// /**
280-
// * Check if the transformer count is the same.
281-
// *
282-
// * @return bool True if the count is the same
283-
// */
284-
// protected function checkTransformerCount(): bool
285-
// {
286-
// // Checking the count alone should be enough
287-
// $cachedTransformerCount = count($this->transformerFilePaths);
288-
// $currentTransformerCount = count(
289-
// $this->transformerMatcher->match($this->className),
290-
// );
291-
// if ($cachedTransformerCount !== $currentTransformerCount) {
292-
// return false;
293-
// }
294-
//
295-
// return true;
296-
// }
297170
}

src/Core/Cache/CacheState/EmptyResultCacheState.php

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
*
1010
* This class is used to represent an empty result cache state, which means that
1111
* the class was not matched by any transformer.
12-
*
13-
* @todo: I think when a transformer is changed, the cache state should be
14-
* invalidated. This is not currently the case. Maybe clear the whole cache
15-
* when a transformer is changed?
1612
*/
1713
class EmptyResultCacheState extends CacheState
1814
{

src/Core/Cache/CacheState/TransformedCacheState.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
2+
/** @noinspection PhpPropertyOnlyWrittenInspection */
33
namespace Okapi\CodeTransformer\Core\Cache\CacheState;
44

55
use Okapi\CodeTransformer\Core\Cache\CacheState;
@@ -11,7 +11,6 @@
1111
*/
1212
class TransformedCacheState extends CacheState
1313
{
14-
1514
public const TRANSFORMED_FILE_PATH_KEY = 'transformedFilePath';
1615
public const TRANSFORMER_FILE_PATHS_KEY = 'transformerFilePaths';
1716

@@ -41,22 +40,18 @@ public function isFresh(): bool
4140
return false;
4241
}
4342

43+
// Check if the transformed file has been deleted
44+
if (!file_exists($this->transformedFilePath)) {
45+
return false;
46+
}
47+
4448
// Check if any of the transformer files have been modified or deleted
4549
foreach ($this->transformerFilePaths as $transformerFilePath) {
4650
if (!file_exists($transformerFilePath)) {
4751
// @codeCoverageIgnoreStart
4852
return false;
4953
// @codeCoverageIgnoreEnd
5054
}
51-
52-
if (filemtime($transformerFilePath) > $this->modificationTime) {
53-
return false;
54-
}
55-
}
56-
57-
// Check if the transformed file has been deleted
58-
if (!file_exists($this->transformedFilePath)) {
59-
return false;
6055
}
6156

6257
return true;

0 commit comments

Comments
 (0)