Skip to content

Commit b9e1c01

Browse files
committed
feat(checks-classses): refactored a little bit the code
1 parent 999e71e commit b9e1c01

File tree

17 files changed

+165
-79
lines changed

17 files changed

+165
-79
lines changed

pint.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"preset": "laravel",
3+
"rules": {
4+
"array_push": true,
5+
"backtick_to_shell_exec": true,
6+
"declare_strict_types": true,
7+
"final_class": true,
8+
"fully_qualified_strict_types": true,
9+
"global_namespace_import": {
10+
"import_classes": true,
11+
"import_constants": true,
12+
"import_functions": true
13+
},
14+
"ordered_class_elements": {
15+
"order": [
16+
"use_trait",
17+
"case",
18+
"constant",
19+
"constant_public",
20+
"constant_protected",
21+
"constant_private",
22+
"property_public",
23+
"property_protected",
24+
"property_private",
25+
"construct",
26+
"destruct",
27+
"magic",
28+
"phpunit",
29+
"method_abstract",
30+
"method_public_static",
31+
"method_public",
32+
"method_protected_static",
33+
"method_protected",
34+
"method_private_static",
35+
"method_private"
36+
],
37+
"sort_algorithm": "none"
38+
},
39+
"ordered_interfaces": true,
40+
"ordered_traits": true,
41+
"protected_to_private": true,
42+
"strict_comparison": true
43+
}
44+
}

src/Checkers/ClassChecker.php

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* @internal
2121
*/
22-
readonly class ClassChecker implements Checker
22+
final readonly class ClassChecker implements Checker
2323
{
2424
/**
2525
* Creates a new instance of ClassChecker.
@@ -70,49 +70,45 @@ public function check(array $parameters): array
7070
*/
7171
private function getIssuesFromClass(SplFileInfo $file): array
7272
{
73-
try {
74-
$class = $this->getClassNameWithNamespace($file);
73+
$class = $this->getClassNameWithNamespace($file);
7574

76-
if ($class === null) {
77-
return [];
78-
}
75+
if ($class === null) {
76+
return [];
77+
}
7978

80-
$reflectionClass = new ReflectionClass($class);
79+
$reflectionClass = new ReflectionClass($class);
8180

81+
$namesToCheck = [
82+
...$this->getMethodNames($reflectionClass),
83+
...$this->getPropertyNames($reflectionClass),
84+
];
85+
86+
if ($docComment = $reflectionClass->getDocComment()) {
8287
$namesToCheck = [
83-
...$this->getMethodNames($reflectionClass),
84-
...$this->getPropertyNames($reflectionClass),
88+
...$namesToCheck,
89+
...explode(PHP_EOL, $docComment),
8590
];
91+
}
8692

87-
if ($docComment = $reflectionClass->getDocComment()) {
88-
$namesToCheck = [
89-
...$namesToCheck,
90-
...explode(PHP_EOL, $docComment),
91-
];
92-
}
93-
94-
if ($namesToCheck === []) {
95-
return [];
96-
}
93+
if ($namesToCheck === []) {
94+
return [];
95+
}
9796

98-
$issues = [];
99-
100-
foreach ($namesToCheck as $name) {
101-
$issues = [
102-
...$issues,
103-
...array_map(
104-
fn (Misspelling $misspelling): Issue => new Issue(
105-
$misspelling,
106-
$file->getRealPath(),
107-
$this->getErrorLine($file, $name),
108-
), $this->spellchecker->check(strtolower((string) preg_replace('/(?<!^)[A-Z]/', ' $0', $name)))),
109-
];
110-
}
97+
$issues = [];
11198

112-
return $issues;
113-
} catch (\Throwable) {
114-
return [];
99+
foreach ($namesToCheck as $name) {
100+
$issues = [
101+
...$issues,
102+
...array_map(
103+
fn (Misspelling $misspelling): Issue => new Issue(
104+
$misspelling,
105+
$file->getRealPath(),
106+
$this->getErrorLine($file, $name),
107+
), $this->spellchecker->check(strtolower((string) preg_replace('/(?<!^)[A-Z]/', ' $0', $name)))),
108+
];
115109
}
110+
111+
return $issues;
116112
}
117113

118114
/**

src/Checkers/FileSystemChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @internal
1616
*/
17-
readonly class FileSystemChecker implements Checker
17+
final readonly class FileSystemChecker implements Checker
1818
{
1919
/**
2020
* Creates a new instance of FileSystemChecker.

src/Console/Commands/DefaultCommand.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @internal
2222
*/
2323
#[AsCommand(name: 'default')]
24-
class DefaultCommand extends Command
24+
final class DefaultCommand extends Command
2525
{
2626
/**
2727
* Executes the command.
@@ -31,7 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3131
$kernel = Kernel::default();
3232

3333
$issues = $kernel->handle([
34-
'directory' => $directory = self::inferProjectPath(),
34+
'directory' => $directory = $this->inferProjectPath(),
3535
]);
3636

3737
$output->writeln('');
@@ -66,7 +66,24 @@ protected function configure(): void
6666
$this->setDescription('Checks for misspellings in the given directory.');
6767
}
6868

69-
protected function renderIssue(OutputInterface $output, Issue $issue, string $currentDirectory): void
69+
/**
70+
* Infer the project's base directory from the environment.
71+
*/
72+
private function inferProjectPath(): string
73+
{
74+
$basePath = dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]);
75+
76+
return match (true) {
77+
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
78+
default => match (true) {
79+
is_dir($basePath.'/src') => ($basePath.'/src'),
80+
is_dir($basePath.'/app') => ($basePath.'/app'),
81+
default => $basePath,
82+
},
83+
};
84+
}
85+
86+
private function renderIssue(OutputInterface $output, Issue $issue, string $currentDirectory): void
7087
{
7188
renderUsing($output);
7289

@@ -88,21 +105,4 @@ protected function renderIssue(OutputInterface $output, Issue $issue, string $cu
88105
</div>
89106
HTML);
90107
}
91-
92-
/**
93-
* Infer the project's base directory from the environment.
94-
*/
95-
protected static function inferProjectPath(): string
96-
{
97-
$basePath = dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]);
98-
99-
return match (true) {
100-
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
101-
default => match (true) {
102-
is_dir($basePath.'/src') => ($basePath.'/src'),
103-
is_dir($basePath.'/app') => ($basePath.'/app'),
104-
default => $basePath,
105-
},
106-
};
107-
}
108108
}

src/Personnnnns.php

Whitespace-only changes.

src/Services/Spellcheckers/InMemorySpellchecker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public function check(string $text): array
5151
/**
5252
* Filters the given words against the whitelisted words stored in the configuration.
5353
*
54-
* @param array<int, \PhpSpellcheck\MisspellingInterface> $misspellings
55-
* @return array<int, \PhpSpellcheck\MisspellingInterface> $misspellings
54+
* @param array<int, MisspellingInterface> $misspellings
55+
* @return array<int, MisspellingInterface> $misspellings
5656
*/
5757
private function filterWhitelistedWords(array $misspellings): array
5858
{

tests/Fixtures/ClassesToTest/ClassWithTypoErrors.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Tests\Fixtures\ClassesToTest;
46

57
/**
@@ -9,7 +11,7 @@
911
*
1012
* @internal
1113
*/
12-
class ClassWithTypoErrors
14+
final class ClassWithTypoErrors
1315
{
1416
public int $propertyWithoutTypoError = 1;
1517

@@ -40,11 +42,11 @@ public function methodWithDocBlockTypoError(): string
4042

4143
public function methodWithTypoErrorInParameters(string $parameterWithoutTypoError, string $parameterWithTypoErorr): string
4244
{
43-
return 'This is a method with a typo error in parameters';
45+
return $parameterWithoutTypoError.$parameterWithTypoErorr.'This is a method with a typo error in parameters';
4446
}
4547

4648
public function methodWithoutTypoErrorInParameters(string $parameterWithoutTypoError): string
4749
{
48-
return 'This is a method without a typo error in parameters';
50+
return $parameterWithoutTypoError.'This is a method without a typo error in parameters';
4951
}
5052
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures\ClassesToTest;
6+
7+
final class ClassWithoutMethodsOrProperties
8+
{
9+
//
10+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Tests\Fixtures\ClassesToTest\FolderThatShouldBeIgnored;
46

5-
class ClassWithTypoErrors
7+
final class ClassWithTypoErrors
68
{
79
public int $properytWithTypoError = 1;
810
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
//

0 commit comments

Comments
 (0)