Skip to content

Commit

Permalink
Merge pull request #645 from phel-lang/add-rector
Browse files Browse the repository at this point in the history
Add rector
  • Loading branch information
JesusValera committed Jan 10, 2024
2 parents 204e1da + 7d4035d commit ccea54c
Show file tree
Hide file tree
Showing 343 changed files with 1,831 additions and 1,439 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -40,6 +40,9 @@ jobs:
- name: Run friendsofphp/php-cs-fixer
run: ./vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run --show-progress=dots --using-cache=no --verbose

- name: Run rector/rector
run: ./vendor/bin/php-cs-fixer fix --dry-run --show-progress=dots --using-cache=no --verbose

type-checker:
name: Type Checker
runs-on: ubuntu-latest
Expand Down
20 changes: 12 additions & 8 deletions .php-cs-fixer.dist.php
Expand Up @@ -18,10 +18,14 @@
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'backtick_to_shell_exec' => true,
'braces' => [
'allow_single_line_closure' => true,
'allow_single_line_anonymous_class_with_empty_body' => true,
],
'single_space_around_construct' => true,
'control_structure_braces' => true,
'control_structure_continuation_position' => true,
'declare_parentheses' => true,
'no_multiple_statements_per_line' => true,
'braces_position' => true,
'statement_indentation' => true,
'no_extra_blank_lines' => true,
'class_definition' => ['single_line' => true],
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true,
Expand All @@ -30,7 +34,7 @@
'ereg_to_preg' => true,
'explicit_string_variable' => true,
'fully_qualified_strict_types' => true,
'function_typehint_space' => true,
'type_declaration_spaces' => true,
'general_phpdoc_annotation_remove' => [
'annotations' => [
'author',
Expand All @@ -48,8 +52,8 @@
'native_function_invocation' => [
'include' => ['@compiler_optimized'],
],
'native_function_type_declaration_casing' => true,
'new_with_braces' => true,
'native_type_declaration_casing' => true,
'new_with_parentheses' => true,
'no_blank_lines_after_class_opening' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
Expand All @@ -61,7 +65,7 @@
'no_multiline_whitespace_around_double_arrow' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_short_bool_cast' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_comma_in_singleline' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_useless_else' => true,
Expand Down
10 changes: 7 additions & 3 deletions composer.json
Expand Up @@ -29,15 +29,16 @@
},
"require-dev": {
"ext-readline": "*",
"friendsofphp/php-cs-fixer": "^3.45",
"friendsofphp/php-cs-fixer": "^3.46",
"infection/infection": "^0.27",
"phpbench/phpbench": "^1.2",
"phpmetrics/phpmetrics": "^2.8",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.6",
"psalm/plugin-phpunit": "^0.18",
"symfony/var-dumper": "^7.0",
"vimeo/psalm": "^5.18"
"vimeo/psalm": "^5.18",
"rector/rector": "^0.18"
},
"autoload": {
"psr-4": {
Expand All @@ -61,7 +62,8 @@
"test-quality": [
"@csrun",
"@psalm",
"@phpstan"
"@phpstan",
"@rectorrun"
],
"static-clear-cache": [
"vendor/bin/psalm --clear-cache",
Expand All @@ -74,6 +76,8 @@
"phpstan": "./vendor/bin/phpstan --memory-limit=516M",
"csfix": "./vendor/bin/php-cs-fixer fix",
"csrun": "./vendor/bin/php-cs-fixer fix --dry-run",
"rector": "./vendor/bin/rector process",
"rectorrun": "./vendor/bin/rector process --dry-run",
"infection": "XDEBUG_MODE=coverage php -d memory_limit=-1 ./vendor/bin/infection --show-mutations --threads=max --min-msi=50 --min-covered-msi=40",
"phpbench": "./vendor/bin/phpbench run --report=aggregate --ansi",
"phpbench-base": "./vendor/bin/phpbench run --tag=baseline --report=aggregate --progress=plain --ansi",
Expand Down
73 changes: 65 additions & 8 deletions composer.lock

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

78 changes: 78 additions & 0 deletions rector.php
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;
use Rector\Core\Collector\MockedClassCollector;
use Rector\Core\Collector\ParentClassCollector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector;

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

$rectorConfig->skip([
__DIR__ . '/tests/php/*/out/*',
__DIR__ . '/tests/php/*/gacela-class-names.php',
__DIR__ . '/tests/php/*/gacela-custom-services.php',

UseClassKeywordForClassNameResolutionRector::class => [
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/LiteralEmitter.php',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/DefEmitter',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/GlobalVarEmitter.php',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/IfEmitter.php',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/MapEmitter.php',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/MethodEmitter.php',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/NsEmitter.php',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/SetVarEmitter.php',
__DIR__ . '/src/php/Compiler/Domain/Emitter/OutputEmitter/NodeEmitter/VectorEmitter.php',
__DIR__ . '/tests/php/Unit/Compiler/Emitter/OutputEmitter/NodeEmitter/ApplyEmitterTest.php',
__DIR__ . '/tests/php/Unit/Compiler/Emitter/OutputEmitter/NodeEmitter/FnAsClassEmitterTest.php',
],

SimplifyIfElseToTernaryRector::class => [
__DIR__ . '/src/php/Lang/Collections/Map/IndexedNode.php',
],

FinalizeClassesWithoutChildrenRector::class => [
__DIR__ . '/src/php/Run/RunFactory.php',
],

PrivatizeFinalClassPropertyRector::class => [
__DIR__ . '/tests/php/Unit/Printer/TypePrinter/StubStruct.php',
],

DisallowedShortTernaryRuleFixerRector::class,
PreferPHPUnitThisCallRector::class,
]);

$rectorConfig->sets([
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::STRICT_BOOLEANS,
SetList::PRIVATIZATION,
SetList::TYPE_DECLARATION,
SetList::EARLY_RETURN,
SetList::INSTANCEOF,
LevelSetList::UP_TO_PHP_82,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::PHPUNIT_90,
]);

$rectorConfig->collector(ParentClassCollector::class);
$rectorConfig->collector(MockedClassCollector::class);

$rectorConfig->importNames();
$rectorConfig->removeUnusedImports();
};
7 changes: 4 additions & 3 deletions src/php/Api/ApiDependencyProvider.php
Expand Up @@ -19,8 +19,9 @@ public function provideModuleDependencies(Container $container): void

private function addRunFacade(Container $container): void
{
$container->set(self::FACADE_RUN, static function (Container $container) {
return $container->getLocator()->get(RunFacade::class);
});
$container->set(
self::FACADE_RUN,
static fn (Container $container) => $container->getLocator()->get(RunFacade::class),
);
}
}
8 changes: 3 additions & 5 deletions src/php/Api/Domain/PhelFnNormalizer.php
Expand Up @@ -11,7 +11,7 @@
final class PhelFnNormalizer implements PhelFnNormalizerInterface
{
public function __construct(
private PhelFnLoaderInterface $phelFnLoader,
private readonly PhelFnLoaderInterface $phelFnLoader,
private array $allNamespaces = [],
) {
}
Expand All @@ -38,7 +38,7 @@ public function getPhelFunctions(array $namespaces = []): array

$doc = $meta[Keyword::create('doc')] ?? '';
$pattern = '#(```phel\n(?<fnSignature>.*)\n```\n)?(?<desc>.*)#s';
preg_match($pattern, $doc, $matches);
preg_match($pattern, (string) $doc, $matches);
$groupKey = $this->groupKey($fnName);

$result[$groupKey][] = new PhelFunction(
Expand Down Expand Up @@ -70,8 +70,6 @@ private function groupKey(string $fnName): string

private function sortingPhelFunctionsCallback(): callable
{
return static function (PhelFunction $a, PhelFunction $b): int {
return $a->fnName() <=> $b->fnName();
};
return static fn (PhelFunction $a, PhelFunction $b): int => $a->fnName() <=> $b->fnName();
}
}
24 changes: 16 additions & 8 deletions src/php/Api/Infrastructure/Command/DocCommand.php
Expand Up @@ -53,9 +53,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function normalizeNamespaces(array $namespaces): array
{
array_walk($namespaces, static function (string &$ns): void {
if (in_array($ns, ['core', 'http', 'html', 'test', 'json']) && !str_starts_with($ns, 'phel\\')) {
$ns = 'phel\\' . $ns;
if (!in_array($ns, ['core', 'http', 'html', 'test', 'json'])) {
return;
}

if (str_starts_with($ns, 'phel\\')) {
return;
}

$ns = 'phel\\' . $ns;
});

return $namespaces;
Expand Down Expand Up @@ -99,8 +105,8 @@ private function normalizeGroupedFunctions(array $phelFunctions, string $search)
$longestFuncNameLength = 5;
$normalized = [];

foreach ($phelFunctions as $function) {
$fnName = $function->fnName();
foreach ($phelFunctions as $phelFunction) {
$fnName = $phelFunction->fnName();
similar_text($fnName, $search, $percent);
if ($search && $percent < 40) {
continue;
Expand All @@ -109,15 +115,17 @@ private function normalizeGroupedFunctions(array $phelFunctions, string $search)
if (strlen($fnName) > $longestFuncNameLength) {
$longestFuncNameLength = strlen($fnName);
}

$normalized[] = [
'percent' => round($percent),
'name' => $fnName,
'signature' => $function->fnSignature(),
'doc' => $function->doc(),
'description' => preg_replace('/\r?\n/', '', $function->description()),
'signature' => $phelFunction->fnSignature(),
'doc' => $phelFunction->doc(),
'description' => preg_replace('/\r?\n/', '', $phelFunction->description()),
];
}
usort($normalized, static fn ($a, $b) => $b['percent'] <=> $a['percent']);

usort($normalized, static fn ($a, $b): int => $b['percent'] <=> $a['percent']);

return [$normalized, $longestFuncNameLength];
}
Expand Down

0 comments on commit ccea54c

Please sign in to comment.