Skip to content

Commit

Permalink
Reduce memory consumption of collectors (TomasVotruba#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ngmy committed Dec 19, 2024
1 parent 574961c commit 884b4a1
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Collectors/Callable_/AttributeCallableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use TomasVotruba\UnusedPublic\ValueObject\ClassAndMethodArrayExprs;

/**
* @implements Collector<AttributeGroup, array<string>|null>
* @implements Collector<AttributeGroup, non-empty-array<string>|null>
*/
final readonly class AttributeCallableCollector implements Collector
{
Expand Down
2 changes: 1 addition & 1 deletion src/Collectors/Callable_/CallUserFuncCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<FuncCall, array<string>|null>
* @implements Collector<FuncCall, non-empty-array<string>|null>
*/
final readonly class CallUserFuncCollector implements Collector
{
Expand Down
6 changes: 3 additions & 3 deletions src/Collectors/ClassConstFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use TomasVotruba\UnusedPublic\ValueObject\ConstantReference;

/**
* @implements Collector<ClassConstFetch, string[]>
* @implements Collector<ClassConstFetch, non-empty-array<string>|null>
*/
final readonly class ClassConstFetchCollector implements Collector
{
Expand All @@ -35,12 +35,12 @@ public function getNodeType(): string

/**
* @param ClassConstFetch $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $this->configuration->isUnusedConstantsEnabled()) {
return [];
return null;
}

if (! $node->class instanceof Name) {
Expand Down
4 changes: 2 additions & 2 deletions src/Collectors/FormTypeClassCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* Match Symfony data_class element in forms types, as those use magic setters/getters
* @implements Collector<ArrayItem, array<string>|null>
* @implements Collector<ArrayItem, non-empty-array<string>|null>
*/
final readonly class FormTypeClassCollector implements Collector
{
Expand All @@ -30,7 +30,7 @@ public function getNodeType(): string

/**
* @param ArrayItem $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Collectors/MethodCall/MethodCallCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<MethodCall, array<string>|null>
* @implements Collector<MethodCall, non-empty-array<string>|null>
*/
final readonly class MethodCallCollector implements Collector
{
Expand Down
2 changes: 1 addition & 1 deletion src/Collectors/MethodCall/MethodCallableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<MethodCallableNode, array<string>|null>
* @implements Collector<MethodCallableNode, non-empty-array<string>|null>
*/
final readonly class MethodCallableCollector implements Collector
{
Expand Down
10 changes: 7 additions & 3 deletions src/Collectors/PublicClassLikeConstCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use TomasVotruba\UnusedPublic\InternalDocStmtAnalyzer;

/**
* @implements Collector<ClassConst, array<array{class-string, string, int}>>
* @implements Collector<ClassConst, non-empty-array<array{class-string, string, int}>|null>
*/
final readonly class PublicClassLikeConstCollector implements Collector
{
Expand All @@ -32,12 +32,12 @@ public function getNodeType(): string

/**
* @param ClassConst $node
* @return array<array{class-string, string, int}>|null
* @return non-empty-array<array{class-string, string, int}>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $this->configuration->isUnusedConstantsEnabled()) {
return [];
return null;
}

if (! $node->isPublic()) {
Expand Down Expand Up @@ -65,6 +65,10 @@ public function processNode(Node $node, Scope $scope): ?array
];
}

if ([] === $constantNames) {
return null;
}

return $constantNames;
}
}
8 changes: 6 additions & 2 deletions src/Collectors/PublicPropertyCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use TomasVotruba\UnusedPublic\InternalDocStmtAnalyzer;

/**
* @implements Collector<InClassNode, array<array{class-string, string, int}>>
* @implements Collector<InClassNode, non-empty-array<array{class-string, string, int}>>
*/
final readonly class PublicPropertyCollector implements Collector
{
Expand All @@ -42,7 +42,7 @@ public function getNodeType(): string

/**
* @param InClassNode $node
* @return array<array{string, string, int}>|null
* @return non-empty-array<array{string, string, int}>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
Expand Down Expand Up @@ -83,6 +83,10 @@ public function processNode(Node $node, Scope $scope): ?array
}
}

if ($publicPropertyNames === []) {
return null;
}

return $publicPropertyNames;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Collectors/PublicPropertyFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use TomasVotruba\UnusedPublic\ValueObject\PropertyReference;

/**
* @implements Collector<PropertyFetch, string[]>
* @implements Collector<PropertyFetch, non-empty-array<string>|null>
*/
final readonly class PublicPropertyFetchCollector implements Collector
{
Expand All @@ -38,7 +38,7 @@ public function getNodeType(): string

/**
* @param PropertyFetch $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
Expand Down Expand Up @@ -79,6 +79,10 @@ public function processNode(Node $node, Scope $scope): ?array
$result = [...$result, (string) $propertyReference, ...$parentPropertyReferences];
}

if ($result === []) {
return null;
}

return $result;
}
}
8 changes: 6 additions & 2 deletions src/Collectors/PublicStaticPropertyFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use TomasVotruba\UnusedPublic\PropertyReference\ParentPropertyReferenceResolver;

/**
* @implements Collector<StaticPropertyFetch, string[]>
* @implements Collector<StaticPropertyFetch, non-empty-array<string>|null>
*/
final readonly class PublicStaticPropertyFetchCollector implements Collector
{
Expand All @@ -34,7 +34,7 @@ public function getNodeType(): string

/**
* @param StaticPropertyFetch $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
Expand Down Expand Up @@ -81,6 +81,10 @@ public function processNode(Node $node, Scope $scope): ?array
$result = [...$result, ...$propertyReferences, ...$parentPropertyReferences];
}

if ($result === []) {
return null;
}

return $result;
}
}
4 changes: 2 additions & 2 deletions src/Collectors/StaticCall/StaticMethodCallCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use TomasVotruba\UnusedPublic\ValueObject\MethodCallReference;

/**
* @implements Collector<StaticCall, array<string>|null>
* @implements Collector<StaticCall, non-empty-array<string>|null>
*/
final readonly class StaticMethodCallCollector implements Collector
{
Expand All @@ -33,7 +33,7 @@ public function getNodeType(): string

/**
* @param StaticCall $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Collectors/StaticCall/StaticMethodCallableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use TomasVotruba\UnusedPublic\ValueObject\MethodCallReference;

/**
* @implements Collector<StaticMethodCallableNode, array<string>|null>
* @implements Collector<StaticMethodCallableNode, non-empty-array<string>|null>
*/
final readonly class StaticMethodCallableCollector implements Collector
{
Expand All @@ -33,7 +33,7 @@ public function getNodeType(): string

/**
* @param StaticMethodCallableNode $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
Expand Down
4 changes: 4 additions & 0 deletions src/Rules/UnusedPublicClassConstRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function processNode(Node $node, Scope $scope): array

foreach ($publicClassLikeConstCollector as $filePath => $declarationsGroups) {
foreach ($declarationsGroups as $declarationGroup) {
if ($declarationGroup === null) {
continue;
}

foreach ($declarationGroup as [$className, $constantName, $line, $isInternal]) {
if ($this->isClassConstantUsed(
$className,
Expand Down

0 comments on commit 884b4a1

Please sign in to comment.