Skip to content

Commit

Permalink
add withPreparePhpCsFixerSets() (#160)
Browse files Browse the repository at this point in the history
* add withPreparePhpCsFixerSets()

* newline
  • Loading branch information
TomasVotruba committed Jan 14, 2024
1 parent 43529c5 commit 39e9a52
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 17 deletions.
77 changes: 77 additions & 0 deletions bin/generate-php-cs-fixer-tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

use PhpCsFixer\RuleSet\RuleSets;

// this helper script generates the withPreparedPhpCsFixerSets() method for ECSConfigBuilder class

require __DIR__ . '/../vendor/autoload.php';

$setsDirectory = __DIR__ . '/../vendor/friendsofphp/php-cs-fixer/src/RuleSet/Sets/';

$setDefinitions = RuleSets::getSetDefinitions();

$setNames = [];
foreach ($setDefinitions as $setDefinition) {
$setNames[] = $setDefinition->getName();
}

// create withPreparedPhpCsFixerSets() method here
$classMethod = new \PhpParser\Node\Stmt\ClassMethod('withPreparedPhpCsFixerSets');
$classMethod->flags = \PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC;
$classMethod->returnType = new \PhpParser\Node\Name('self');

foreach ($setNames as $setName) {
// convert to PHP variable name
$paramName = ltrim( $setName, '@');

$paramName = lowercaseUntilFirstLower($paramName);
$paramName = str_replace(':r', 'R', $paramName);
$paramName = str_replace(['.', '-', '_'], '', $paramName);

// lowercase only the first uppercase letters

$classMethod->params[] = new \PhpParser\Node\Param(
new \PhpParser\Node\Expr\Variable($paramName),
new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('false')),
new \PhpParser\Node\Identifier('bool')
);

$dynamicSetsPropertyFetch = new \PhpParser\Node\Expr\PropertyFetch(new \PhpParser\Node\Expr\Variable('this'), 'dynamicSets');

$classMethod->stmts[] = new \PhpParser\Node\Stmt\If_(new \PhpParser\Node\Expr\Variable($paramName), [
'stmts' => [
new \PhpParser\Node\Stmt\Expression(new \PhpParser\Node\Expr\Assign(
new \PhpParser\Node\Expr\ArrayDimFetch($dynamicSetsPropertyFetch),
new \PhpParser\Node\Scalar\String_($setName)
))
]
]);
}


function lowercaseUntilFirstLower($input) {
$output = '';
$foundLower = false;

for ($i = 0; $i < strlen($input); $i++) {
$char = $input[$i];

if (!$foundLower && ctype_upper($char)) {
$output .= strtolower($char);
} else {
$output .= $char;
$foundLower = true;
}
}

return $output;
}
// add dynamic set includes

$classMethod->stmts[] = new \PhpParser\Node\Stmt\Return_(new \PhpParser\Node\Expr\Variable('this'));


$printerStandard = new \PhpParser\PrettyPrinter\Standard();
echo $printerStandard->prettyPrint([$classMethod]);
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-webmozart-assert": "^1.2",
"phpunit/phpunit": "^10.5",
"rector/rector": "^0.18.13",
"rector/rector": "^0.19",
"symplify/phpstan-extensions": "^11.4",
"symplify/vendor-patches": "^11.3",
"tomasvotruba/class-leak": "^0.2",
Expand Down
9 changes: 3 additions & 6 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php

declare(strict_types=1);

use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([
__DIR__ . '/config',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPaths([__DIR__ . '/config', __DIR__ . '/src', __DIR__ . '/tests'])
->withRules([LineLengthFixer::class])
->withRootFiles()
->withPreparedSets(psr12: true, common: true);
11 changes: 2 additions & 9 deletions scoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@
'exclude-files' => [...$polyfillsBootstraps, ...$polyfillsStubs],

// expose
'expose-classes' => [
'Normalizer',
],
'expose-classes' => ['Normalizer'],

'patchers' => [
static function (string $filePath, string $prefix, string $content): string {
Expand All @@ -82,12 +80,7 @@ static function (string $filePath, string $prefix, string $content): string {
$prefix . '\\\\ORM\\\\Mapping\\\\Entity',
$prefix . '\\\\Mapping\\\\Entity',
$prefix . '\\\\ODM\\\\Document',
], [
'ORM\\Entity',
'ORM\\Mapping\\Entity',
'Mapping\\Entity',
'ODM\\Document',
], $content);
], ['ORM\\Entity', 'ORM\\Mapping\\Entity', 'Mapping\\Entity', 'ODM\\Document'], $content);
},

static function (string $filePath, string $prefix, string $content): string {
Expand Down
Loading

0 comments on commit 39e9a52

Please sign in to comment.