Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare class-names case insensitive #42

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Filtering/PossiblyUnusedClassesFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ public function filter(
$typesToSkip = [...$typesToSkip, ...self::DEFAULT_TYPES_TO_SKIP];
$attributesToSkip = [...$attributesToSkip, ...self::DEFAULT_ATTRIBUTES_TO_SKIP];

$usedClassNames = array_map('strtolower', $usedClassNames);
$suffixesToSkip = array_map('strtolower', $suffixesToSkip);

foreach ($filesWithClasses as $fileWithClass) {
if (in_array($fileWithClass->getClassName(), $usedClassNames, true)) {
$lowerClassName = strtolower($fileWithClass->getClassName());

if (in_array($lowerClassName, $usedClassNames, true)) {
continue;
}

Expand All @@ -94,7 +99,7 @@ public function filter(

// is excluded suffix?
foreach ($suffixesToSkip as $suffixToSkip) {
if (str_ends_with($fileWithClass->getClassName(), $suffixToSkip)) {
if (str_ends_with($lowerClassName, $suffixToSkip)) {
continue 2;
}
}
Expand Down
63 changes: 63 additions & 0 deletions tests/Filtering/PossiblyUnusedClassesFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\ClassLeak\Tests\Filtering;

use Iterator;
use TomasVotruba\ClassLeak\Filtering\PossiblyUnusedClassesFilter;
use TomasVotruba\ClassLeak\Tests\AbstractTestCase;
use TomasVotruba\ClassLeak\ValueObject\FileWithClass;

final class PossiblyUnusedClassesFilterTest extends AbstractTestCase
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to include this test as a single test fixture in the rule itselft, instead of adding a new unit test.
It would be easier to read for me later.

Copy link
Contributor Author

@staabm staabm Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the class is used at the top level CheckCommand which means I would need to test at the command level.. with all its dependencies?
are you sure we want that ? :)

$possiblyUnusedFilesWithClasses = $this->possiblyUnusedClassesFilter->filter(
$existingFilesWithClasses,
$usedNames,
$typesToSkip,
$suffixesToSkip,
$attributesToSkip
);

Copy link
Owner

@TomasVotruba TomasVotruba Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. What I really want is the test to be simpler, as reading is quite complex now :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On other hand, we should not encourage using wrong class casing. This should be spotted and trigger the class name fix instead.

Copy link
Contributor Author

@staabm staabm Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan itself does already error about class-name-case-mismatches. I think its not the job of class-leak to report it as a problem

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, this tool builds on top of PHPStan best quality code. Let PHPStan handle these 👍

{
/**
* @param FileWithClass[] $filesWithClasses
* @param string[] $usedClassNames
* @param string[] $typesToSkip
* @param string[] $suffixesToSkip
* @param string[] $attributesToSkip
*
* @dataProvider provideData
*/
public function testFilter(
array $expectedResult,
array $filesWithClasses,
array $usedClassNames,
array $typesToSkip,
array $suffixesToSkip,
array $attributesToSkip
): void
{
$possiblyUnusedClassesFilter = new PossiblyUnusedClassesFilter();
$this->assertSame($expectedResult, $possiblyUnusedClassesFilter->filter(
$filesWithClasses,
$usedClassNames,
$typesToSkip,
$suffixesToSkip,
$attributesToSkip
));
}

public static function provideData(): Iterator
{
yield [ // test case-insensitive class name
[],
[new FileWithClass('foo.php', 'Bar', false, [])],
['BAR'],
[],
[],
[]
];

yield [ // test case-insensitive skip suffixes
[],
[new FileWithClass('foo.php', 'Bar', false, [])],
[],
[],
['AR'],
[]
];

}
}
Loading