Skip to content

Commit a0ea403

Browse files
authored
Merge pull request #520 from goaop/feature/modernize-php-code
feat: Modernise php code
2 parents 3a8fc15 + 7856409 commit a0ea403

File tree

6 files changed

+36
-41
lines changed

6 files changed

+36
-41
lines changed

src/Core/AspectKernel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,15 @@ protected function normalizeOptions(array $options): array
224224
}
225225

226226
$rawExcludePaths = is_array($merged['excludePaths'] ?? null) ? $merged['excludePaths'] : [];
227-
$excludePaths = array_values(array_filter($rawExcludePaths, 'is_string'));
227+
$excludePaths = array_values(array_filter($rawExcludePaths, is_string(...)));
228228
$excludePaths[] = $cacheDir;
229229
$excludePaths[] = __DIR__ . '/../';
230230

231231
$appDir = is_string($merged['appDir'] ?? null) ? $merged['appDir'] : '';
232232
$cacheFileMode = is_int($merged['cacheFileMode'] ?? null) ? $merged['cacheFileMode'] : (0770 & ~umask());
233233
$features = is_int($merged['features'] ?? null) ? $merged['features'] : 0;
234234
$rawIncludePaths = is_array($merged['includePaths'] ?? null) ? $merged['includePaths'] : [];
235-
$includePaths = array_values(array_filter($rawIncludePaths, 'is_string'));
235+
$includePaths = array_values(array_filter($rawIncludePaths, is_string(...)));
236236
$debug = is_bool($merged['debug'] ?? null) ? $merged['debug'] : false;
237237

238238
$containerClass = static::$containerClass;
@@ -256,8 +256,8 @@ protected function normalizeOptions(array $options): array
256256
'cacheDir' => $resolvedCacheDir,
257257
'cacheFileMode' => $cacheFileMode,
258258
'features' => $features,
259-
'includePaths' => array_values(array_filter($resolvedIncludePaths, 'is_string')),
260-
'excludePaths' => array_values(array_filter($resolvedExcludePaths, 'is_string')),
259+
'includePaths' => array_values(array_filter($resolvedIncludePaths, is_string(...))),
260+
'excludePaths' => array_values(array_filter($resolvedExcludePaths, is_string(...))),
261261
'containerClass' => $containerClass,
262262
];
263263
}

src/Core/IntroductionAspectExtension.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,28 @@ protected function getAdvice(
7575
Aspect $aspect,
7676
ReflectionProperty $aspectProperty
7777
): Advice {
78-
$pointcutExpression = $interceptorAttribute->expression;
79-
switch (true) {
80-
case ($interceptorAttribute instanceof DeclareError):
81-
$errorMessage = $aspectProperty->getDefaultValue();
82-
if (!is_string($errorMessage) || $errorMessage === '') {
83-
throw new \UnexpectedValueException('DeclareError property must have a non-empty string default value');
84-
}
85-
return new DeclareErrorInterceptor($errorMessage, $interceptorAttribute->level, $pointcutExpression);
86-
87-
case ($interceptorAttribute instanceof DeclareParents):
88-
return new TraitIntroductionInfo($interceptorAttribute->trait, $interceptorAttribute->interface);
78+
return match (true) {
79+
$interceptorAttribute instanceof DeclareError =>
80+
$this->createDeclareErrorAdvice($aspectProperty, $interceptorAttribute),
81+
$interceptorAttribute instanceof DeclareParents =>
82+
new TraitIntroductionInfo($interceptorAttribute->trait, $interceptorAttribute->interface),
83+
default =>
84+
throw new UnexpectedValueException('Unsupported attribute class: ' . get_class($interceptorAttribute)),
85+
};
86+
}
8987

90-
default:
91-
throw new UnexpectedValueException('Unsupported attribute class: ' . get_class($interceptorAttribute));
88+
/**
89+
* Creates a DeclareErrorInterceptor after validating the property's default value.
90+
*
91+
* @throws \UnexpectedValueException if the property default value is not a non-empty string
92+
*/
93+
private function createDeclareErrorAdvice(ReflectionProperty $aspectProperty, DeclareError $attribute): DeclareErrorInterceptor
94+
{
95+
$errorMessage = $aspectProperty->getDefaultValue();
96+
if (!is_string($errorMessage) || $errorMessage === '') {
97+
throw new \UnexpectedValueException('DeclareError property must have a non-empty string default value');
9298
}
99+
100+
return new DeclareErrorInterceptor($errorMessage, $attribute->level, $attribute->expression);
93101
}
94102
}

src/Core/LazyAdvisorAccessor.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,13 @@
2525
#[AllowDynamicProperties]
2626
class LazyAdvisorAccessor
2727
{
28-
/**
29-
* Instance of aspect container
30-
*/
31-
protected AspectContainer $container;
32-
33-
/**
34-
* Aspect loader instance
35-
*/
36-
protected AspectLoader $loader;
37-
3828
/**
3929
* Accessor constructor
4030
*/
41-
public function __construct(AspectContainer $container, AspectLoader $loader)
42-
{
43-
$this->container = $container;
44-
$this->loader = $loader;
45-
}
31+
public function __construct(
32+
protected AspectContainer $container,
33+
protected AspectLoader $loader
34+
) {}
4635

4736
/**
4837
* Magic advice accessor

src/Instrument/PathResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class PathResolver
3131
*
3232
* @return ($somePath is array ? list<string|false> : string|false)
3333
*/
34-
public static function realpath($somePath, bool $shouldCheckExistence = false)
34+
public static function realpath(string|array $somePath, bool $shouldCheckExistence = false): string|array|false
3535
{
36-
// Do not resolve empty string/false/arrays into the current path
36+
// Do not resolve empty string/empty arrays into the current path
3737
if (!$somePath) {
3838
return $somePath;
3939
}

src/Instrument/Transformer/CachingTransformer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class CachingTransformer extends BaseSourceTransformer
3131
protected int $cacheFileMode = 0770;
3232

3333
/**
34-
* @var SourceTransformer[]|callable(): SourceTransformer[]
34+
* @var SourceTransformer[]|Closure(): SourceTransformer[]
3535
*/
36-
protected $transformers = [];
36+
protected array|Closure $transformers = [];
3737

3838
/**
3939
* Cache manager
@@ -43,9 +43,9 @@ class CachingTransformer extends BaseSourceTransformer
4343
/**
4444
* Class constructor
4545
*
46-
* @param SourceTransformer[]|callable(): SourceTransformer[] $transformers Source transformers or callable that should return transformers
46+
* @param SourceTransformer[]|Closure(): SourceTransformer[] $transformers Source transformers or closure that should return transformers
4747
*/
48-
public function __construct(AspectKernel $kernel, $transformers, CachePathManager $cacheManager)
48+
public function __construct(AspectKernel $kernel, array|Closure $transformers, CachePathManager $cacheManager)
4949
{
5050
parent::__construct($kernel);
5151
$this->cacheManager = $cacheManager;

src/Instrument/Transformer/StreamMetaData.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ class StreamMetaData
5151

5252
/**
5353
* Wrapper-specific data attached to this stream.
54-
*
55-
* @var mixed
5654
*/
57-
public $wrapperData;
55+
public mixed $wrapperData;
5856

5957
/**
6058
* Array containing the names of any filters that have been stacked onto this stream.

0 commit comments

Comments
 (0)