Skip to content

Commit

Permalink
Enable Coding Style Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanzweifel committed Feb 20, 2024
1 parent db6f948 commit 699fd14
Show file tree
Hide file tree
Showing 27 changed files with 67 additions and 61 deletions.
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
])
// uncomment to reach your current PHP version
->withPhpSets(php80: true)
->withPreparedSets(deadCode: true)
->withPreparedSets(deadCode: true, codingStyle: true)
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
]);
7 changes: 4 additions & 3 deletions src/ClassesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function findAndLoadClasses(): Collection
if ($this->isMostLikelyPestTest($file)) {
return true;
}

require_once $file->getRealPath();
} catch (Exception) {

Check warning on line 31 in src/ClassesFinder.php

View check run for this annotation

Codecov / codecov/patch

src/ClassesFinder.php#L31

Added line #L31 was not covered by tests
//
Expand All @@ -35,7 +36,7 @@ public function findAndLoadClasses(): Collection
ob_end_clean();

return collect(get_declared_classes())
->reject(fn (string $className) => Str::startsWith($className, ['SwooleLibrary']));
->reject(static fn(string $className) => Str::startsWith($className, ['SwooleLibrary']));
}

/**
Expand All @@ -58,7 +59,7 @@ protected function findFilesInProjectPath(): Collection
*/
protected function isExcluded(SplFileInfo $file, Collection $excludes): bool
{
return $excludes->contains(fn ($exclude) => Str::startsWith($file->getPathname(), $exclude));
return $excludes->contains(static fn($exclude) => Str::startsWith($file->getPathname(), $exclude));
}

/**
Expand Down Expand Up @@ -96,7 +97,7 @@ protected function isMostLikelyPestTest(SplFileInfo $file): bool
'afterAll',
]);

if (preg_match("/$methodNames\s*\(/", $fileContent)) {
if (preg_match(sprintf('/%s\s*\(/', $methodNames), $fileContent)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Classifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getClassifierForClassInstance(ReflectionClass $class): Classifie
$classifierInstance = new $classifier();

if (! $this->implementsContract($classifier)) {
throw new Exception("Classifier {$classifier} does not implement ".ClassifierContract::class.'.');
throw new Exception(sprintf('Classifier %s does not implement ', $classifier).ClassifierContract::class.'.');
}

try {
Expand Down
6 changes: 2 additions & 4 deletions src/Classifiers/ControllerClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ public function name(): string
public function satisfies(ReflectionClass $class): bool
{
return collect(app(Router::class)->getRoutes())
->reject(function ($route) {
->reject(static function ($route) {
if (method_exists($route, 'getActionName')) {
// Laravel
return $route->getActionName() === 'Closure';
}

// Lumen
return data_get($route, 'action.uses') === null;
})
->map(function ($route) {
->map(static function ($route) {
if (method_exists($route, 'getController')) {
// Laravel
try {
Expand All @@ -36,7 +35,6 @@ public function satisfies(ReflectionClass $class): bool
return;
}
}

// Lumen
return Str::before(data_get($route, 'action.uses'), '@');
})
Expand Down
4 changes: 3 additions & 1 deletion src/Classifiers/MiddlewareClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ protected function getMiddlewares(): array
{
$reflection = new ReflectionProperty($this->httpKernel, 'middleware');
$reflection->setAccessible(true);

$middleware = $reflection->getValue($this->httpKernel);

$reflection = new ReflectionProperty($this->httpKernel, 'routeMiddleware');
$reflection->setAccessible(true);

$routeMiddlwares = $reflection->getValue($this->httpKernel);

return array_values(array_unique(array_merge($middleware, $routeMiddlwares)));
Expand Down Expand Up @@ -89,7 +91,7 @@ private function getRouteMiddlewares(): array
$router = $reflection->getValue($this->httpKernel);

return collect($router->getRoutes()->getRoutes())
->map(fn (Route $route) => $route->middleware())
->map(static fn(Route $route) => $route->middleware())
->flatten()
->unique()
->toArray();
Expand Down
6 changes: 3 additions & 3 deletions src/Classifiers/ObserverClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function name(): string
public function satisfies(ReflectionClass $class): bool
{
return collect($this->getEvents())
->filter(fn ($_listeners, $event) => Str::startsWith($event, 'eloquent.'))
->filter(static fn($_listeners, $event) => Str::startsWith($event, 'eloquent.'))
->map(fn ($listeners) => collect($listeners)->map(fn ($closure) => $this->getEventListener($closure))->toArray())
->collapse()
->unique()
->filter(fn ($eventListener) => is_string($eventListener))
->filter(fn (string $eventListenerSignature) => Str::contains($eventListenerSignature, $class->getName()))
->filter(static fn($eventListener) => is_string($eventListener))
->filter(static fn(string $eventListenerSignature) => Str::contains($eventListenerSignature, $class->getName()))
->count() > 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/Classifiers/ResourceClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function satisfies(ReflectionClass $class): bool
if ($class->isSubclassOf(JsonResource::class)) {
return true;
}

return $class->isSubclassOf(ResourceCollection::class);
}

Expand Down
10 changes: 4 additions & 6 deletions src/Console/StatsListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,20 @@ public function handle(): void
// Remove Classes based on the RejectionStrategy
// Remove Classes based on the namespace
$reflectionClasses = $classes
->map(fn ($class) => new ReflectionClass($class))
->reject(fn (ReflectionClass $class) => app(config('stats.rejection_strategy', RejectVendorClasses::class))
->map(static fn($class) => new ReflectionClass($class))
->reject(static fn(ReflectionClass $class) => app(config('stats.rejection_strategy', RejectVendorClasses::class))
->shouldClassBeRejected($class))
->unique(fn (ReflectionClass $class) => $class->getFileName())
->reject(function (ReflectionClass $class) {
->unique(static fn(ReflectionClass $class) => $class->getFileName())
->reject(static function (ReflectionClass $class) {
// Never discard anonymous database migrations
if (Str::contains($class->getName(), 'Migration@anonymous')) {
return false;
}

foreach (config('stats.ignored_namespaces', []) as $namespace) {
if (Str::startsWith($class->getNamespaceName(), $namespace)) {
return true;

Check warning on line 57 in src/Console/StatsListCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Console/StatsListCommand.php#L56-L57

Added lines #L56 - L57 were not covered by tests
}
}

return false;
});

Expand Down
13 changes: 7 additions & 6 deletions src/Outputs/AsciiTableOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public function render(Project $project, bool $isVerbose = false, array $filterB
->setHeaders(['Name', 'Classes', 'Methods', 'Methods/Class', 'LoC', 'LLoC', 'LLoC/Method']);

// Render "Core" components
$this->renderComponents($table, $groupedByComponent->filter(fn ($_, $key) => $key !== 'Other' && ! Str::contains($key, 'Test')));
$this->renderComponents($table, $groupedByComponent->filter(static fn($_, $key) => $key !== 'Other' && ! Str::contains($key, 'Test')));

// Render Test components
$this->renderComponents($table, $groupedByComponent->filter(fn ($_, $key) => Str::contains($key, 'Test')));
$this->renderComponents($table, $groupedByComponent->filter(static fn($_, $key) => Str::contains($key, 'Test')));

// Render "Other" component
$this->renderComponents($table, $groupedByComponent->filter(fn ($_, $key) => $key === 'Other'));
$this->renderComponents($table, $groupedByComponent->filter(static fn($_, $key) => $key === 'Other'));

$table->addRow(new TableSeparator);
$this->addTotalRow($table);
Expand All @@ -80,6 +80,7 @@ private function renderComponents(Table $table, Collection $groupedByComponent):
foreach ($classifiedClasses as $classifiedClass) {
$this->addClassifiedClassTableRow($table, $classifiedClass);
}

$table->addRow(new TableSeparator);
}
}
Expand Down Expand Up @@ -129,16 +130,16 @@ private function addTotalRow(Table $table): void
private function addMetaRow(Table $table): void
{
$table->setFooterTitle(implode('', [
"Code LLoC: {$this->project->statistic()->getLogicalLinesOfCodeForApplicationCode()}",
"Test LLoC: {$this->project->statistic()->getLogicalLinesOfCodeForTestCode()}",
'Code LLoC: ' . $this->project->statistic()->getLogicalLinesOfCodeForApplicationCode(),
'Test LLoC: ' . $this->project->statistic()->getLogicalLinesOfCodeForTestCode(),
'Code/Test Ratio: 1:'.$this->project->statistic()->getApplicationCodeToTestCodeRatio(),
'Routes: '.app(NumberOfRoutes::class)->get(),
]));
}

private function rightAlignNumbers(Table $table): void
{
for ($i = 1; $i <= 6; $i++) {
for ($i = 1; $i <= 6; ++$i) {
$table->setColumnStyle($i, (new TableStyle)->setPadType(STR_PAD_LEFT));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
private Collection $classes
) {
// Loop through ReflectionClasses and classify them.
$this->classifiedClasses = $this->classes->map(fn (ReflectionClass $reflectionClass) => new ClassifiedClass(
$this->classifiedClasses = $this->classes->map(static fn(ReflectionClass $reflectionClass) => new ClassifiedClass(
$reflectionClass,
app(Classifier::class)->getClassifierForClassInstance($reflectionClass)
));
Expand All @@ -33,8 +33,8 @@ public function classifiedClasses(): Collection
public function classifiedClassesGroupedByComponentName(): Collection
{
return $this->classifiedClasses()
->groupBy(fn (ClassifiedClass $classifiedClass) => $classifiedClass->classifier->name())
->sortBy(fn ($_, string $componentName) => $componentName);
->groupBy(static fn(ClassifiedClass $classifiedClass) => $classifiedClass->classifier->name())
->sortBy(static fn($_, string $componentName) => $componentName);
}

public function classifiedClassesGroupedAndFilteredByComponentNames(array $componentNamesToFilter = []): Collection
Expand All @@ -44,7 +44,7 @@ public function classifiedClassesGroupedAndFilteredByComponentNames(array $compo
return $this->classifiedClassesGroupedByComponentName()
->when(
$shouldCollectionBeFiltered,
fn (Collection $components) => $components->filter(fn ($_item, string $key) => in_array($key, $componentNamesToFilter))
static fn(Collection $components) => $components->filter(static fn($_item, string $key) => in_array($key, $componentNamesToFilter))
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function isVendorProvided(): bool
public function usesTrait(string $name): bool
{
return collect($this->getTraits())
->contains(fn (NativeReflectionClass $trait) => $trait->name == $name);
->contains(static fn(NativeReflectionClass $trait) => $trait->name == $name);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Statistics/ProjectStatistic.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNumberOfClasses(): int
public function getNumberOfMethods(): int
{
if ($this->numberOfMethods === null) {
$this->numberOfMethods = $this->project->classifiedClasses()->sum(fn (ClassifiedClass $class) => $class->getNumberOfMethods());
$this->numberOfMethods = $this->project->classifiedClasses()->sum(static fn(ClassifiedClass $class) => $class->getNumberOfMethods());
}

return $this->numberOfMethods;
Expand All @@ -71,7 +71,7 @@ public function getNumberOfMethodsPerClass(): float
public function getLinesOfCode(): int
{
if ($this->linesOfCode === null) {
$this->linesOfCode = $this->project->classifiedClasses()->sum(fn (ClassifiedClass $class) => $class->getLines());
$this->linesOfCode = $this->project->classifiedClasses()->sum(static fn(ClassifiedClass $class) => $class->getLines());
}

return $this->linesOfCode;
Expand All @@ -80,7 +80,7 @@ public function getLinesOfCode(): int
public function getLogicalLinesOfCode(): float
{
if ($this->logicalLinesOfCode === null) {
$this->logicalLinesOfCode = $this->project->classifiedClasses()->sum(fn (ClassifiedClass $class) => $class->getLogicalLinesOfCode());
$this->logicalLinesOfCode = $this->project->classifiedClasses()->sum(static fn(ClassifiedClass $class) => $class->getLogicalLinesOfCode());
}

return $this->logicalLinesOfCode;
Expand All @@ -104,17 +104,17 @@ public function getLogicalLinesOfCodeForApplicationCode(): float
return $this
->project
->classifiedClasses()
->filter(fn (ClassifiedClass $classifiedClass) => $classifiedClass->classifier->countsTowardsApplicationCode())
->sum(fn (ClassifiedClass $class) => $class->getLogicalLinesOfCode());
->filter(static fn(ClassifiedClass $classifiedClass) => $classifiedClass->classifier->countsTowardsApplicationCode())
->sum(static fn(ClassifiedClass $class) => $class->getLogicalLinesOfCode());
}

public function getLogicalLinesOfCodeForTestCode(): float
{
return $this
->project
->classifiedClasses()
->filter(fn (ClassifiedClass $classifiedClass) => $classifiedClass->classifier->countsTowardsTests())
->sum(fn (ClassifiedClass $class) => $class->getLogicalLinesOfCode());
->filter(static fn(ClassifiedClass $classifiedClass) => $classifiedClass->classifier->countsTowardsTests())
->sum(static fn(ClassifiedClass $class) => $class->getLogicalLinesOfCode());
}

public function getApplicationCodeToTestCodeRatio(): float
Expand Down
6 changes: 3 additions & 3 deletions src/ValueObjects/ClassifiedClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getNumberOfPublicMethods(): int
{
if ($this->numberOfPublicMethods === null) {
$this->numberOfPublicMethods = $this->reflectionClass->getDefinedMethods()
->filter(fn (ReflectionMethod $method) => $method->isPublic())->count();
->filter(static fn(ReflectionMethod $method) => $method->isPublic())->count();
}

return $this->numberOfPublicMethods;
Expand All @@ -83,7 +83,7 @@ public function getNumberOfNonPublicMethods(): int
{
if ($this->numberOfNonPublicMethods === null) {
$this->numberOfNonPublicMethods = $this->reflectionClass->getDefinedMethods()
->filter(fn (ReflectionMethod $method) => ! $method->isPublic())->count();
->filter(static fn(ReflectionMethod $method) => ! $method->isPublic())->count();
}

return $this->numberOfNonPublicMethods;
Expand Down Expand Up @@ -122,7 +122,7 @@ public function getLogicalLinesOfCodePerMethod(): float
{
if ($this->logicalLinesOfCodePerMethod === null) {
if ($this->getNumberOfMethods() === 0) {
$this->logicalLinesOfCodePerMethod = $this->logicalLinesOfCodePerMethod = 0;
$this->logicalLinesOfCodePerMethod = 0;
} else {
$this->logicalLinesOfCodePerMethod = round($this->getLogicalLinesOfCode() / $this->getNumberOfMethods(), 2);
}
Expand Down
10 changes: 5 additions & 5 deletions src/ValueObjects/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getNumberOfClasses(): int
public function getNumberOfMethods(): int
{
if ($this->numberOfMethods === null) {
$this->numberOfMethods = $this->classifiedClasses->sum(fn (ClassifiedClass $class) => $class->getNumberOfMethods());
$this->numberOfMethods = $this->classifiedClasses->sum(static fn(ClassifiedClass $class) => $class->getNumberOfMethods());
}

return $this->numberOfMethods;
Expand All @@ -73,7 +73,7 @@ public function getNumberOfMethods(): int
public function getNumberOfPublicMethods(): int
{
if ($this->numberOfPublicMethods === null) {
$this->numberOfPublicMethods = $this->classifiedClasses->sum(fn (ClassifiedClass $class) => $class->getNumberOfPublicMethods());
$this->numberOfPublicMethods = $this->classifiedClasses->sum(static fn(ClassifiedClass $class) => $class->getNumberOfPublicMethods());
}

return $this->numberOfPublicMethods;
Expand All @@ -82,7 +82,7 @@ public function getNumberOfPublicMethods(): int
public function getNumberOfNonPublicMethods(): int
{
if ($this->numberOfNonPublicMethods === null) {
$this->numberOfNonPublicMethods = $this->classifiedClasses->sum(fn (ClassifiedClass $class) => $class->getNumberOfNonPublicMethods());
$this->numberOfNonPublicMethods = $this->classifiedClasses->sum(static fn(ClassifiedClass $class) => $class->getNumberOfNonPublicMethods());
}

return $this->numberOfNonPublicMethods;
Expand All @@ -100,7 +100,7 @@ public function getNumberOfMethodsPerClass(): float
public function getLinesOfCode(): int
{
if ($this->linesOfCode === null) {
$this->linesOfCode = $this->classifiedClasses->sum(fn (ClassifiedClass $class) => $class->getLines());
$this->linesOfCode = $this->classifiedClasses->sum(static fn(ClassifiedClass $class) => $class->getLines());
}

return $this->linesOfCode;
Expand All @@ -109,7 +109,7 @@ public function getLinesOfCode(): int
public function getLogicalLinesOfCode(): float
{
if ($this->logicalLinesOfCode === null) {
$this->logicalLinesOfCode = $this->classifiedClasses->sum(fn (ClassifiedClass $class) => $class->getLogicalLinesOfCode());
$this->logicalLinesOfCode = $this->classifiedClasses->sum(static fn(ClassifiedClass $class) => $class->getLogicalLinesOfCode());
}

return $this->logicalLinesOfCode;
Expand Down
2 changes: 1 addition & 1 deletion tests/ClassesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ClassesFinderTest extends TestCase
{
public function setUp() : void
protected function setUp() : void
{
parent::setUp();

Expand Down
2 changes: 1 addition & 1 deletion tests/Classifiers/MiddlewareClassifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MiddlewareClassifierTest extends TestCase
*/
protected function usesMiddlewareRoutes($router)
{
$router->get('/demo', fn () => 'Hello World')->middleware(DemoMiddleware::class);
$router->get('/demo', static fn() => 'Hello World')->middleware(DemoMiddleware::class);
}

#[Test]
Expand Down
2 changes: 1 addition & 1 deletion tests/Console/StatsListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class StatsListCommandTest extends TestCase
{
public function setUp() : void
protected function setUp() : void
{
parent::setUp();

Expand Down
2 changes: 1 addition & 1 deletion tests/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function creates_a_project_object_from_a_collection_of_reflection_classes
$project = new Project($classes);

$this->assertTrue(
$project->classifiedClasses()->map(fn ($class) => $class->reflectionClass)->contains($projectModel)
$project->classifiedClasses()->map(static fn($class) => $class->reflectionClass)->contains($projectModel)
);
$this->assertInstanceOf(ClassifiedClass::class, $project->classifiedClasses()->first());
}
Expand Down
Loading

0 comments on commit 699fd14

Please sign in to comment.