Skip to content

Commit

Permalink
Fix issues identified by PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 26, 2024
1 parent 2bdd27d commit 667fa29
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/CodeExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ private function exportVariable(mixed $variable): string
return 'unserialize(' . var_export(serialize($variable), true) . ')';
}

/**
* @param array<mixed> $array
*/
private function arrayOnlyContainsScalars(array $array): bool
{
$result = true;
Expand Down
55 changes: 49 additions & 6 deletions src/ExcludeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,80 @@

final class ExcludeList
{
private array $globalVariables = [];
private array $classes = [];
/**
* @var array<non-empty-string, true>
*/
private array $globalVariables = [];

/**
* @var list<non-empty-string>
*/
private array $classes = [];

/**
* @var list<non-empty-string>
*/
private array $classNamePrefixes = [];
private array $parentClasses = [];
private array $interfaces = [];
private array $staticProperties = [];

/**
* @var list<non-empty-string>
*/
private array $parentClasses = [];

/**
* @var list<non-empty-string>
*/
private array $interfaces = [];

/**
* @var array<string, array<non-empty-string, true>>
*/
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])) {
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Restorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
108 changes: 97 additions & 11 deletions src/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,17 +38,61 @@
final class Snapshot
{
private ExcludeList $excludeList;
private array $globalVariables = [];
private array $superGlobalArrays = [];

/**
* @var array<string, mixed>
*/
private array $globalVariables = [];

/**
* @var list<string>
*/
private array $superGlobalArrays = [];

/**
* @var array<string, array<string, mixed>>
*/
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<string, array<string, mixed>>
*/
private array $staticProperties = [];

/**
* @var array<non-empty-string, array{global_value: string, local_value: string, access: int}>
*/
private array $iniSettings = [];

/**
* @var list<string>
*/
private array $includedFiles = [];

/**
* @var array<string, mixed>
*/
private array $constants = [];

/**
* @var list<callable-string>
*/
private array $functions = [];

/**
* @var list<class-string>
*/
private array $interfaces = [];

/**
* @var list<class-string>
*/
private array $classes = [];

/**
* @var list<class-string>
*/
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)
{
Expand Down Expand Up @@ -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) {
Expand All @@ -96,56 +145,89 @@ public function excludeList(): ExcludeList
return $this->excludeList;
}

/**
* @return array<string, mixed>
*/
public function globalVariables(): array
{
return $this->globalVariables;
}

/**
* @return array<string, array<string, mixed>>
*/
public function superGlobalVariables(): array
{
return $this->superGlobalVariables;
}

/**
* @return list<string>
*/
public function superGlobalArrays(): array
{
return $this->superGlobalArrays;
}

/**
* @return array<string, array<string, mixed>>
*/
public function staticProperties(): array
{
return $this->staticProperties;
}

/**
* @return array<non-empty-string, array{global_value: string, local_value: string, access: int}>
*/
public function iniSettings(): array
{
return $this->iniSettings;
}

/**
* @return list<string>
*/
public function includedFiles(): array
{
return $this->includedFiles;
}

/**
* @return array<string, mixed>
*/
public function constants(): array
{
return $this->constants;
}

/**
* @return list<callable-string>
*/
public function functions(): array
{
return $this->functions;
}

/**
* @return list<class-string>
*/
public function interfaces(): array
{
return $this->interfaces;
}

/**
* @return list<class-string>
*/
public function classes(): array
{
return $this->classes;
}

/**
* @return list<class-string>
*/
public function traits(): array
{
return $this->traits;
Expand Down Expand Up @@ -307,6 +389,9 @@ private function canBeSerialized(mixed $variable): bool
return true;
}

/**
* @return array<mixed>
*/
private function enumerateObjectsAndResources(mixed $variable, Context $processed = new Context): array
{
$result = [];
Expand All @@ -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;
Expand Down

0 comments on commit 667fa29

Please sign in to comment.