-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add withPreparePhpCsFixerSets() (#160)
* add withPreparePhpCsFixerSets() * newline
- Loading branch information
1 parent
43529c5
commit 39e9a52
Showing
5 changed files
with
362 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.