Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d24aea7
commit a8fe5ab
Showing
4 changed files
with
134 additions
and
90 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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package: 'CodeQuality' | ||
name: 'SimplifyMirrorAssignRector' # w/wo suffix | ||
package: "CodeQuality" | ||
name: "SimplifyMirrorAssignRector" | ||
node_types: | ||
- 'Assign' | ||
- "Assign" # put main node first | ||
|
||
description: 'Removes unneeded $a = $a assigns' | ||
description: "Removes unneeded $a = $a assigns" | ||
code_before: "$a = $a;" | ||
code_after: "" | ||
source: "" # e.g. link to RFC or headline in upgrade guide | ||
level: "" # e.g. symfony30.yml, target config to append this rector to |
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
84 changes: 84 additions & 0 deletions
84
packages/ContributorTools/src/TemplateVariablesFactory.php
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,84 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\ContributorTools; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use Rector\ContributorTools\Configuration\Configuration; | ||
use Rector\Printer\BetterStandardPrinter; | ||
use function Safe\sprintf; | ||
|
||
final class TemplateVariablesFactory | ||
{ | ||
/** | ||
* @var BetterStandardPrinter | ||
*/ | ||
private $betterStandardPrinter; | ||
|
||
public function __construct(BetterStandardPrinter $betterStandardPrinter) | ||
{ | ||
$this->betterStandardPrinter = $betterStandardPrinter; | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function createFromConfiguration(Configuration $configuration): array | ||
{ | ||
$data = [ | ||
'_Package_' => $configuration->getPackage(), | ||
'_Category_' => $configuration->getCategory(), | ||
'_Description_' => $configuration->getDescription(), | ||
'_Name_' => $configuration->getName(), | ||
'_CodeBefore_' => trim($configuration->getCodeBefore()) . PHP_EOL, | ||
'_CodeBeforeExample_' => $this->createCodeForDefinition($configuration->getCodeBefore()), | ||
'_CodeAfter_' => trim($configuration->getCodeAfter()) . PHP_EOL, | ||
'_CodeAfterExample_' => $this->createCodeForDefinition($configuration->getCodeAfter()), | ||
'_Source_' => $this->createSourceDocBlock($configuration->getSource()), | ||
]; | ||
|
||
$data['_NodeTypes_Php_'] = $this->createNodeTypePhp($configuration); | ||
$data['_NodeTypes_Doc_'] = '\\' . implode('|\\', $configuration->getNodeTypes()); | ||
|
||
return $data; | ||
} | ||
|
||
private function createCodeForDefinition(string $code): string | ||
{ | ||
if (Strings::contains($code, PHP_EOL)) { | ||
// multi lines | ||
return sprintf("<<<'CODE_SAMPLE'%s%sCODE_SAMPLE%s", PHP_EOL, $code, PHP_EOL); | ||
} | ||
|
||
// single line | ||
return "'" . str_replace("'", '"', $code) . "'"; | ||
} | ||
|
||
private function createSourceDocBlock(string $source): string | ||
{ | ||
if (! $source) { | ||
return $source; | ||
} | ||
|
||
$sourceDocBlock = <<<'CODE_SAMPLE' | ||
/** | ||
* @see %s | ||
*/ | ||
CODE_SAMPLE; | ||
|
||
return sprintf($sourceDocBlock, $source); | ||
} | ||
|
||
private function createNodeTypePhp(Configuration $configuration): string | ||
{ | ||
$arrayNodes = []; | ||
foreach ($configuration->getNodeTypes() as $nodeType) { | ||
$arrayNodes[] = new ArrayItem(new ClassConstFetch(new FullyQualified($nodeType), 'class')); | ||
} | ||
|
||
return $this->betterStandardPrinter->prettyPrint([new Array_($arrayNodes)]); | ||
} | ||
} |