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

Add rector #308

Merged
merged 12 commits into from
Dec 23, 2023
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'encoding' => true,
'ereg_to_preg' => true,
'explicit_string_variable' => true,
'fully_qualified_strict_types' => true,
'fully_qualified_strict_types' => false,
'type_declaration_spaces' => true,
'general_phpdoc_annotation_remove' => [
'annotations' => [
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
"infection/infection": "^0.26",
"phpbench/phpbench": "^1.2",
"phpmetrics/phpmetrics": "^2.8",
"phpunit/phpunit": "^9.6",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6",
"psalm/plugin-phpunit": "^0.18",
"rector/rector": "^0.18",
"symfony/console": "^5.4",
"symfony/var-dumper": "^5.4",
"vimeo/psalm": "^5.18"
Expand Down
100 changes: 78 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$rectorConfig->sets([
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::PRIVATIZATION,
SetList::TYPE_DECLARATION,
SetList::EARLY_RETURN,
SetList::INSTANCEOF,
]);
};
4 changes: 2 additions & 2 deletions src/Console/ConsoleDependencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function provideModuleDependencies(Container $container): void

private function addCommands(Container $container): void
{
$container->set(self::COMMANDS, static fn () => [
$container->set(self::COMMANDS, static fn (): array => [
new MakeFileCommand(),
new MakeModuleCommand(),
new ListModulesCommand(),
Expand All @@ -37,7 +37,7 @@ private function addCommands(Container $container): void

private function addTemplateByFilenameMap(Container $container): void
{
$container->set(self::TEMPLATE_BY_FILENAME_MAP, fn () => [
$container->set(self::TEMPLATE_BY_FILENAME_MAP, fn (): array => [
FilenameSanitizer::FACADE => $this->getConfig()->getFacadeMakerTemplate(),
FilenameSanitizer::FACTORY => $this->getConfig()->getFactoryMakerTemplate(),
FilenameSanitizer::CONFIG => $this->getConfig()->getConfigMakerTemplate(),
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Domain/AllAppModules/AllAppModulesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@
/** @var SplFileInfo $fileInfo */
foreach ($this->fileIterator as $fileInfo) {
$appModule = $this->createAppModule($fileInfo, $filter);
if ($appModule !== null && $this->isFacade($appModule)) {
if ($appModule instanceof AppModule && $this->isFacade($appModule)) {
$result[$appModule->facadeClass()] = $appModule;
}
}
uksort($result, static fn ($a, $b) => $a <=> $b);

uksort($result, static fn ($a, $b): int => $a <=> $b);

Check warning on line 35 in src/Console/Domain/AllAppModules/AllAppModulesFinder.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ $result[$appModule->facadeClass()] = $appModule; } } - uksort($result, static fn($a, $b): int => $a <=> $b); + return array_values($result); } private function createAppModule(SplFileInfo $fileInfo, string $filter) : ?AppModule

return array_values($result);
}

private function createAppModule(SplFileInfo $fileInfo, string $filter): ?AppModule
{
if (!$fileInfo->isFile()

Check warning on line 42 in src/Console/Domain/AllAppModules/AllAppModulesFinder.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ } private function createAppModule(SplFileInfo $fileInfo, string $filter) : ?AppModule { - if (!$fileInfo->isFile() || $fileInfo->getExtension() !== 'php' || str_contains($fileInfo->getRealPath(), 'vendor' . DIRECTORY_SEPARATOR)) { + if (!$fileInfo->isFile() && $fileInfo->getExtension() !== 'php' || str_contains($fileInfo->getRealPath(), 'vendor' . DIRECTORY_SEPARATOR)) { return null; } $namespace = $this->getNamespace($fileInfo);
|| $fileInfo->getExtension() !== 'php'
|| str_contains($fileInfo->getRealPath(), 'vendor' . DIRECTORY_SEPARATOR)

Check warning on line 44 in src/Console/Domain/AllAppModules/AllAppModulesFinder.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } private function createAppModule(SplFileInfo $fileInfo, string $filter) : ?AppModule { - if (!$fileInfo->isFile() || $fileInfo->getExtension() !== 'php' || str_contains($fileInfo->getRealPath(), 'vendor' . DIRECTORY_SEPARATOR)) { + if (!$fileInfo->isFile() || $fileInfo->getExtension() !== 'php' || str_contains($fileInfo->getRealPath(), DIRECTORY_SEPARATOR . 'vendor')) { return null; } $namespace = $this->getNamespace($fileInfo);

Check warning on line 44 in src/Console/Domain/AllAppModules/AllAppModulesFinder.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } private function createAppModule(SplFileInfo $fileInfo, string $filter) : ?AppModule { - if (!$fileInfo->isFile() || $fileInfo->getExtension() !== 'php' || str_contains($fileInfo->getRealPath(), 'vendor' . DIRECTORY_SEPARATOR)) { + if (!$fileInfo->isFile() || $fileInfo->getExtension() !== 'php' || str_contains($fileInfo->getRealPath(), 'vendor')) { return null; } $namespace = $this->getNamespace($fileInfo);
) {
return null;
}
Expand Down Expand Up @@ -70,7 +71,7 @@

private function getNamespace(SplFileInfo $fileInfo): string
{
$fileContent = (string)file_get_contents($fileInfo->getRealPath());

Check warning on line 74 in src/Console/Domain/AllAppModules/AllAppModulesFinder.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ } private function getNamespace(SplFileInfo $fileInfo) : string { - $fileContent = (string) file_get_contents($fileInfo->getRealPath()); + $fileContent = file_get_contents($fileInfo->getRealPath()); preg_match('#namespace (.*);#', $fileContent, $matches); return $matches[1] ?? ''; }

preg_match('#namespace (.*);#', $fileContent, $matches);

Expand All @@ -82,7 +83,7 @@
$pieces = explode(DIRECTORY_SEPARATOR, $fileInfo->getFilename());
$filename = end($pieces);

return substr($filename, 0, strpos($filename, '.') ?: 1);

Check warning on line 86 in src/Console/Domain/AllAppModules/AllAppModulesFinder.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ { $pieces = explode(DIRECTORY_SEPARATOR, $fileInfo->getFilename()); $filename = end($pieces); - return substr($filename, 0, strpos($filename, '.') ?: 1); + return substr($filename, 0, strpos($filename, '.') ?: 0); } private function isFacade(AppModule $appModule) : bool {

Check warning on line 86 in src/Console/Domain/AllAppModules/AllAppModulesFinder.php

View workflow job for this annotation

GitHub Actions / Mutation Tests

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ { $pieces = explode(DIRECTORY_SEPARATOR, $fileInfo->getFilename()); $filename = end($pieces); - return substr($filename, 0, strpos($filename, '.') ?: 1); + return substr($filename, 0, strpos($filename, '.') ?: 2); } private function isFacade(AppModule $appModule) : bool {
}

private function isFacade(AppModule $appModule): bool
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Domain/AllAppModules/AppModuleCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ private function findDependencyProvider(string $facadeClass): ?string
if ((new ReflectionClass($resolver))->isAnonymous()) {
throw new DependencyProviderNotFoundException($resolver);
}

return $resolver::class;
} catch (DependencyProviderNotFoundException $e) {
} catch (DependencyProviderNotFoundException $dependencyProviderNotFoundException) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public static function noAutoloadPsr4Found(): self
*/
public static function noAutoloadPsr4MatchFound(string $desiredNamespace, array $knownPsr4 = []): self
{
$parsedKnownPsr4 = array_map(static fn (string $p) => str_replace('\\', '', $p), $knownPsr4);
$parsedKnownPsr4 = array_map(
static fn (string $p): string => str_replace('\\', '', $p),
$knownPsr4,
);

return new self(
sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function parse(string $desiredNamespace): CommandArguments
if (isset($psr4[$psr4Key])) {
return $this->foundPsr4($psr4Key, $psr4[$psr4Key], $desiredNamespace);
}

if (isset($psr4Dev[$psr4Key])) {
return $this->foundPsr4($psr4Key, $psr4Dev[$psr4Key], $desiredNamespace);
}
Expand All @@ -72,7 +73,7 @@ private function allPossiblePsr4Combinations(string $desiredNamespace): array
$result = [];

foreach (explode('/', $desiredNamespace) as $explodedArg) {
if (empty($result)) {
if ($result === []) {
$result[] = $explodedArg;
} else {
$prevValue = $result[count($result) - 1];
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Domain/FileContent/FileContentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function generate(CommandArguments $commandArguments, string $filename, b

$template = $this->templateByFilenameMap[$filename] ?? '';
if (empty($template)) {
throw new RuntimeException("Unknown template for '{$filename}'?");
throw new RuntimeException(sprintf("Unknown template for '%s'?", $filename));
}

$fileContent = str_replace($search, $replace, $template);
Expand Down
3 changes: 3 additions & 0 deletions src/Console/Domain/FilenameSanitizer/FilenameSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
final class FilenameSanitizer implements FilenameSanitizerInterface
{
public const FACADE = 'Facade';

public const FACTORY = 'Factory';

public const CONFIG = 'Config';

public const DEPENDENCY_PROVIDER = 'DependencyProvider';

public const EXPECTED_FILENAMES = [
Expand Down
1 change: 1 addition & 0 deletions src/Console/Infrastructure/Command/ListModulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class ListModulesCommand extends Command
use DocBlockResolverAwareTrait;

private const CHECK_SYMBOL = '✔️';

private const CROSS_SYMBOL = '✖️';

private ?OutputInterface $output = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Infrastructure/Command/MakeFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$filename,
$shortName,
);
$output->writeln("> Path '{$absolutePath}' created successfully");
$output->writeln(sprintf("> Path '%s' created successfully", $absolutePath));
}

return self::SUCCESS;
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Infrastructure/Command/MakeModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$filename,
$shortName,
);
$output->writeln("> Path '{$fullPath}' created successfully");
$output->writeln(sprintf("> Path '%s' created successfully", $fullPath));
}

$pieces = explode('/', $commandArguments->directory());
$moduleName = end($pieces);
$output->writeln("Module '{$moduleName}' created successfully");
$output->writeln(sprintf("Module '%s' created successfully", $moduleName));

return self::SUCCESS;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Console/Infrastructure/FileContentIo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ public function mkdir(string $directory): void
if (is_dir($directory)) {
return;
}
if (!mkdir($directory) && !is_dir($directory)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $directory));

if (mkdir($directory)) {
return;
}

if (is_dir($directory)) {
return;
}

throw new RuntimeException(sprintf('Directory "%s" was not created', $directory));
}

public function filePutContents(string $path, string $fileContent): void
Expand Down
Loading
Loading