From 667fa29ed05f4c74088eba6a26f578b4a61a713b Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Wed, 26 Jun 2024 09:01:14 +0200 Subject: [PATCH] Fix issues identified by PHPStan --- src/CodeExporter.php | 3 ++ src/ExcludeList.php | 55 +++++++++++++++++++--- src/Restorer.php | 2 +- src/Snapshot.php | 108 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 150 insertions(+), 18 deletions(-) diff --git a/src/CodeExporter.php b/src/CodeExporter.php index 6b72081..d3238e5 100644 --- a/src/CodeExporter.php +++ b/src/CodeExporter.php @@ -85,6 +85,9 @@ private function exportVariable(mixed $variable): string return 'unserialize(' . var_export(serialize($variable), true) . ')'; } + /** + * @param array $array + */ private function arrayOnlyContainsScalars(array $array): bool { $result = true; diff --git a/src/ExcludeList.php b/src/ExcludeList.php index 6f193c3..07c29a0 100644 --- a/src/ExcludeList.php +++ b/src/ExcludeList.php @@ -15,38 +15,80 @@ final class ExcludeList { - private array $globalVariables = []; - private array $classes = []; + /** + * @var array + */ + private array $globalVariables = []; + + /** + * @var list + */ + private array $classes = []; + + /** + * @var list + */ private array $classNamePrefixes = []; - private array $parentClasses = []; - private array $interfaces = []; - private array $staticProperties = []; + /** + * @var list + */ + private array $parentClasses = []; + + /** + * @var list + */ + private array $interfaces = []; + + /** + * @var array> + */ + private array $staticProperties = []; + + /** + * @param non-empty-string $variableName + */ public function addGlobalVariable(string $variableName): void { $this->globalVariables[$variableName] = true; } + /** + * @param non-empty-string $className + */ public function addClass(string $className): void { $this->classes[] = $className; } + /** + * @param non-empty-string $className + */ public function addSubclassesOf(string $className): void { $this->parentClasses[] = $className; } + /** + * @param non-empty-string $interfaceName + */ public function addImplementorsOf(string $interfaceName): void { $this->interfaces[] = $interfaceName; } + /** + * @param non-empty-string $classNamePrefix + */ public function addClassNamePrefix(string $classNamePrefix): void { $this->classNamePrefixes[] = $classNamePrefix; } + /** + * @param non-empty-string $className + * @param non-empty-string $propertyName + */ public function addStaticProperty(string $className, string $propertyName): void { if (!isset($this->staticProperties[$className])) { @@ -62,7 +104,8 @@ public function isGlobalVariableExcluded(string $variableName): bool } /** - * @param class-string $className + * @param class-string $className + * @param non-empty-string $propertyName */ public function isStaticPropertyExcluded(string $className, string $propertyName): bool { diff --git a/src/Restorer.php b/src/Restorer.php index d84e155..2a08a5e 100644 --- a/src/Restorer.php +++ b/src/Restorer.php @@ -33,7 +33,7 @@ public function restoreGlobalVariables(Snapshot $snapshot): void foreach (array_keys($GLOBALS) as $key) { if ($key !== 'GLOBALS' && !in_array($key, $superGlobalArrays, true) && - !$snapshot->excludeList()->isGlobalVariableExcluded($key)) { + !$snapshot->excludeList()->isGlobalVariableExcluded((string) $key)) { if (array_key_exists($key, $globalVariables)) { $GLOBALS[$key] = $globalVariables[$key]; } else { diff --git a/src/Snapshot.php b/src/Snapshot.php index 7e828dd..e8b7a16 100644 --- a/src/Snapshot.php +++ b/src/Snapshot.php @@ -12,6 +12,7 @@ use function array_keys; use function array_merge; use function array_reverse; +use function assert; use function get_declared_classes; use function get_declared_interfaces; use function get_declared_traits; @@ -37,17 +38,61 @@ final class Snapshot { private ExcludeList $excludeList; - private array $globalVariables = []; - private array $superGlobalArrays = []; + + /** + * @var array + */ + private array $globalVariables = []; + + /** + * @var list + */ + private array $superGlobalArrays = []; + + /** + * @var array> + */ private array $superGlobalVariables = []; - private array $staticProperties = []; - private array $iniSettings = []; - private array $includedFiles = []; - private array $constants = []; - private array $functions = []; - private array $interfaces = []; - private array $classes = []; - private array $traits = []; + + /** + * @var array> + */ + private array $staticProperties = []; + + /** + * @var array + */ + private array $iniSettings = []; + + /** + * @var list + */ + private array $includedFiles = []; + + /** + * @var array + */ + private array $constants = []; + + /** + * @var list + */ + private array $functions = []; + + /** + * @var list + */ + private array $interfaces = []; + + /** + * @var list + */ + private array $classes = []; + + /** + * @var list + */ + private array $traits = []; public function __construct(?ExcludeList $excludeList = null, bool $includeGlobalVariables = true, bool $includeStaticProperties = true, bool $includeConstants = true, bool $includeFunctions = true, bool $includeClasses = true, bool $includeInterfaces = true, bool $includeTraits = true, bool $includeIniSettings = true, bool $includeIncludedFiles = true) { @@ -79,7 +124,11 @@ public function __construct(?ExcludeList $excludeList = null, bool $includeGloba } if ($includeIniSettings) { - $this->iniSettings = ini_get_all(null, false); + $iniSettings = ini_get_all(null, false); + + assert($iniSettings !== false); + + $this->iniSettings = $iniSettings; } if ($includeIncludedFiles) { @@ -96,56 +145,89 @@ public function excludeList(): ExcludeList return $this->excludeList; } + /** + * @return array + */ public function globalVariables(): array { return $this->globalVariables; } + /** + * @return array> + */ public function superGlobalVariables(): array { return $this->superGlobalVariables; } + /** + * @return list + */ public function superGlobalArrays(): array { return $this->superGlobalArrays; } + /** + * @return array> + */ public function staticProperties(): array { return $this->staticProperties; } + /** + * @return array + */ public function iniSettings(): array { return $this->iniSettings; } + /** + * @return list + */ public function includedFiles(): array { return $this->includedFiles; } + /** + * @return array + */ public function constants(): array { return $this->constants; } + /** + * @return list + */ public function functions(): array { return $this->functions; } + /** + * @return list + */ public function interfaces(): array { return $this->interfaces; } + /** + * @return list + */ public function classes(): array { return $this->classes; } + /** + * @return list + */ public function traits(): array { return $this->traits; @@ -307,6 +389,9 @@ private function canBeSerialized(mixed $variable): bool return true; } + /** + * @return array + */ private function enumerateObjectsAndResources(mixed $variable, Context $processed = new Context): array { $result = []; @@ -321,6 +406,7 @@ private function enumerateObjectsAndResources(mixed $variable, Context $processe $processed->add($variable); if (is_array($variable)) { + /** @phpstan-ignore foreach.nonIterable */ foreach ($array as $element) { if (!is_array($element) && !is_object($element) && !is_resource($element)) { continue;