Skip to content

Commit

Permalink
Merge pull request #3 from okapi-web/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
WalterWoshid authored Apr 20, 2023
2 parents d29c599 + 964fe58 commit 8969d3b
Show file tree
Hide file tree
Showing 30 changed files with 577 additions and 677 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ij_php_align_assignments = true
ij_php_align_class_constants = true
ij_php_align_enum_cases = true
ij_php_align_key_value_pairs = true
ij_php_align_multiline_parameters_in_calls = true
ij_php_align_multiline_parameters_in_calls = false
ij_php_align_phpdoc_comments = true
ij_php_align_phpdoc_param_names = true
ij_php_blank_lines_before_package = 0
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ composer.lock
# PHPUnit
tests/coverage/
tests/cache/
tests/tmp/
.phpunit.result.cache
12 changes: 3 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "okapi/code-transformer",
"description": "PHP Code Transformer is a PHP library that allows you to modify and transform the source code of a loaded PHP class.",
"version": "1.2.0",
"version": "1.3.0",
"type": "library",
"homepage": "https://github.com/okapi-web/php-code-transformer",
"license": "MIT",
Expand Down Expand Up @@ -33,7 +33,7 @@
"roave/better-reflection": "^6.8"
},
"require-dev": {
"phpunit/phpunit": "dev-main",
"phpunit/phpunit": "^10",
"symfony/var-dumper": "^6.2"
},
"autoload": {
Expand All @@ -48,11 +48,5 @@
"psr-4": {
"Okapi\\CodeTransformer\\Tests\\": "tests/"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/WalterWoshid/phpunit"
}
]
}
}
9 changes: 4 additions & 5 deletions src/Core/AutoloadInterceptor/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function loadClass($namespacedClass): bool
/**
* Find the path to the file and match and apply the transformers.
*
* @param $namespacedClass
* @param class-string $namespacedClass
*
* @return false|string
*
Expand All @@ -105,7 +105,6 @@ public function findFile($namespacedClass): false|string
$filePath = Path::resolve($filePath);



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

Expand Down Expand Up @@ -145,11 +144,11 @@ protected function isInternal(string $namespacedClass): bool
'Okapi\\CodeTransformer\\',
'Okapi\\Path\\',
'Okapi\\Wildcards\\',
'DI\\'
'DI\\',
],
[
'Okapi\\CodeTransformer\\Tests\\'
]
'Okapi\\CodeTransformer\\Tests\\',
],
);
}
}
193 changes: 33 additions & 160 deletions src/Core/Cache/CacheState.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
abstract class CacheState
{
public const TYPE = 'type';
public const DATA = 'data';

public const ORIGINAL_FILE_PATH_KEY = 'originalFilePath';
public const NAMESPACED_CLASS_KEY = 'namespacedClass';
public const MODIFICATION_TIME_KEY = 'modificationTime';

public string $originalFilePath;
public int $modificationTime;
protected string $namespacedClass;
protected int $modificationTime;

/**
* CacheState constructor.
Expand Down Expand Up @@ -80,18 +83,39 @@ public function getRequiredKeys(): array
{
return [
static::ORIGINAL_FILE_PATH_KEY,
static::NAMESPACED_CLASS_KEY,
static::MODIFICATION_TIME_KEY,
];
}

/**
* Create a cache state if it is valid.
*
* @param array $cacheStateArray
*
* @return self|null
*/
public function createIfValid(array $cacheStateArray): ?CacheState
{
if (!$this->valid($cacheStateArray)) {
// @codeCoverageIgnoreStart
return null;
// @codeCoverageIgnoreEnd
}

$this->setData($cacheStateArray);

return $this;
}

/**
* Validate the cache state.
*
* @param array<string, (string|int|string[])> $cacheStateArray
*
* @return bool
*/
public function valid(array $cacheStateArray): bool
private function valid(array $cacheStateArray): bool
{
// Check if all required keys are present
foreach ($this->getRequiredKeys() as $requiredKey) {
Expand All @@ -110,7 +134,7 @@ public function valid(array $cacheStateArray): bool
*
* @param array<string, (string|int|string[])> $cacheStateArray
*/
public function setData(array $cacheStateArray): void
private function setData(array $cacheStateArray): void
{
foreach ($cacheStateArray as $key => $value) {
$this->{$key} = $value;
Expand All @@ -124,6 +148,12 @@ public function setData(array $cacheStateArray): void
*/
public function isFresh(): bool
{
if (!file_exists($this->originalFilePath)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

if (filemtime($this->originalFilePath) > $this->modificationTime) {
return false;
}
Expand All @@ -137,161 +167,4 @@ public function isFresh(): bool
* @return string|null
*/
abstract public function getFilePath(): ?string;

// /**
// * CacheState constructor.
// *
// * @param string $originalFilePath
// * @param string $className
// * @param string|null $cachedFilePath
// * @param int|null $transformedTime
// * @param string[]|null $transformerFilePaths
// */
// public function __construct(
// public string $originalFilePath,
// public string $className,
// public ?string $cachedFilePath,
// public ?int $transformedTime,
// public ?array $transformerFilePaths,
// ) {}
//
// /**
// * Use the cached file path if aspects have been applied.
// * Otherwise, use the original file path if no aspects have been applied.
// *
// * @return string
// */
// public function getFilePath(): string
// {
// return $this->cachedFilePath ?? $this->originalFilePath;
// }
//
//
//
//
// /**
// * Get the cache state as an array.
// *
// * @return array
// */
// public function toArray(): array
// {
// return [
// $this->originalFilePath,
// $this->className,
// $this->cachedFilePath,
// $this->transformedTime,
// $this->transformerFilePaths,
// ];
// }
//
// /**
// * Check if the cache is not outdated.
// *
// * @return bool
// */
// public function isFresh(): bool
// {
// // @codeCoverageIgnoreStart
// // This should only happen if the project is misconfigured
// if ($this->checkInfiniteLoop()) {
// return false;
// }
// // @codeCoverageIgnoreEnd
//
// $allFiles = array_merge(
// [$this->originalFilePath],
// $this->transformerFilePaths,
// );
//
// if ($this->checkFilesModified($allFiles)) {
// return false;
// }
//
// if ($this->cachedFilePath) {
// $allFiles[] = $this->cachedFilePath;
// }
//
// if (!$this->checkFilesExist($allFiles)) {
// return false;
// }
//
// if (!$this->checkTransformerCount()) {
// return false;
// }
//
// return true;
// }
//
// /**
// * Check if the cache is in an infinite loop.
// *
// * @return bool True if the cache is in an infinite loop
// */
// protected function checkInfiniteLoop(): bool
// {
// if ($this->cachedFilePath !== null) {
// // Same original file and cached file
// if ($this->originalFilePath === $this->cachedFilePath) {
// return true;
// }
// }
//
// return false;
// }
//
// /**
// * Check if the files have been modified.
// *
// * @param string[] $files
// *
// * @return bool True if any file has been modified
// */
// protected function checkFilesModified(array $files): bool
// {
// $lastModified = max(array_map('filemtime', $files));
// if ($lastModified >= $this->transformedTime) {
// return true;
// }
//
// return false;
// }
//
// /**
// * Check if the files exist.
// *
// * @param string[] $files
// *
// * @return bool True if all files exist
// */
// protected function checkFilesExist(array $files): bool
// {
// // Check if the cache file exists
// foreach ($files as $file) {
// if (!file_exists($file)) {
// return false;
// }
// }
//
// return true;
// }
//
// /**
// * Check if the transformer count is the same.
// *
// * @return bool True if the count is the same
// */
// protected function checkTransformerCount(): bool
// {
// // Checking the count alone should be enough
// $cachedTransformerCount = count($this->transformerFilePaths);
// $currentTransformerCount = count(
// $this->transformerMatcher->match($this->className),
// );
// if ($cachedTransformerCount !== $currentTransformerCount) {
// return false;
// }
//
// return true;
// }
}
4 changes: 0 additions & 4 deletions src/Core/Cache/CacheState/EmptyResultCacheState.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
*
* This class is used to represent an empty result cache state, which means that
* the class was not matched by any transformer.
*
* @todo: I think when a transformer is changed, the cache state should be
* invalidated. This is not currently the case. Maybe clear the whole cache
* when a transformer is changed?
*/
class EmptyResultCacheState extends CacheState
{
Expand Down
17 changes: 6 additions & 11 deletions src/Core/Cache/CacheState/TransformedCacheState.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

/** @noinspection PhpPropertyOnlyWrittenInspection */
namespace Okapi\CodeTransformer\Core\Cache\CacheState;

use Okapi\CodeTransformer\Core\Cache\CacheState;
Expand All @@ -11,7 +11,6 @@
*/
class TransformedCacheState extends CacheState
{

public const TRANSFORMED_FILE_PATH_KEY = 'transformedFilePath';
public const TRANSFORMER_FILE_PATHS_KEY = 'transformerFilePaths';

Expand Down Expand Up @@ -41,22 +40,18 @@ public function isFresh(): bool
return false;
}

// Check if the transformed file has been deleted
if (!file_exists($this->transformedFilePath)) {
return false;
}

// Check if any of the transformer files have been modified or deleted
foreach ($this->transformerFilePaths as $transformerFilePath) {
if (!file_exists($transformerFilePath)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}

if (filemtime($transformerFilePath) > $this->modificationTime) {
return false;
}
}

// Check if the transformed file has been deleted
if (!file_exists($this->transformedFilePath)) {
return false;
}

return true;
Expand Down
Loading

0 comments on commit 8969d3b

Please sign in to comment.