Skip to content

Commit

Permalink
Reduce memory consumption of collectors (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Sep 16, 2024
1 parent 0d3003f commit c82b690
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 @@ -15,7 +15,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 @@ -15,7 +15,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<ClassConstFetch, string[]>
* @implements Collector<ClassConstFetch, non-empty-array<string>|null>
*/
final readonly class ClassConstFetchCollector implements Collector
{
Expand All @@ -32,12 +32,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 @@ -17,7 +17,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 @@ -16,7 +16,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 @@ -13,7 +13,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @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 @@ -30,12 +30,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 All @@ -56,6 +56,10 @@ public function processNode(Node $node, Scope $scope): ?array
$constantNames[] = [$classReflection->getName(), $constConst->name->toString(), $node->getLine()];
}

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 @@ -15,7 +15,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @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 @@ -40,7 +40,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 @@ -79,6 +79,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 @@ -15,7 +15,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<PropertyFetch, string[]>
* @implements Collector<PropertyFetch, non-empty-array<string>|null>
*/
final readonly class PublicPropertyFetchCollector implements Collector
{
Expand All @@ -35,7 +35,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 @@ -70,6 +70,10 @@ public function processNode(Node $node, Scope $scope): ?array
$result[] = $propertyReflection->getDeclaringClass()->getName() . '::' . $propertyName;
}

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 @@ -15,7 +15,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<StaticPropertyFetch, string[]>
* @implements Collector<StaticPropertyFetch, non-empty-array<string>|null>
*/
final readonly class PublicStaticPropertyFetchCollector implements Collector
{
Expand All @@ -32,7 +32,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 @@ -73,6 +73,10 @@ public function processNode(Node $node, Scope $scope): ?array
$result[] = $propertyReflection->getDeclaringClass()->getName() . '::' . $propertyName;
}

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 @@ -15,7 +15,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<StaticCall, array<string>|null>
* @implements Collector<StaticCall, non-empty-array<string>|null>
*/
final readonly class StaticMethodCallCollector implements Collector
{
Expand All @@ -32,7 +32,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 @@ -15,7 +15,7 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<StaticMethodCallableNode, array<string>|null>
* @implements Collector<StaticMethodCallableNode, non-empty-array<string>|null>
*/
final readonly class StaticMethodCallableCollector implements Collector
{
Expand All @@ -32,7 +32,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 @@ -67,6 +67,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]) {
if ($this->isClassConstantUsed(
$className,
Expand Down

0 comments on commit c82b690

Please sign in to comment.