add File suffix and specific variable content case

This commit is contained in:
Tomas Votruba 2018-08-29 12:17:33 +02:00
parent 9f2dbb7822
commit e09bfd06b7
8 changed files with 50 additions and 7 deletions

View File

@ -8,6 +8,9 @@ use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Constant\ConstantStringType;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Printer\BetterStandardPrinter;
use Rector\Rector\AbstractRector;
@ -76,12 +79,30 @@ final class ParseFileRector extends AbstractRector
private function isArgumentYamlFile(StaticCall $staticCallNode): bool
{
$possibleFileNode = $staticCallNode->args[0]->value;
$possibleFileNodeAsString = $this->betterStandardPrinter->prettyPrint([$possibleFileNode]);
if (Strings::match($possibleFileNodeAsString, '#yml|yaml(\'|")$#')) {
// is yml/yaml file
if (Strings::match($possibleFileNodeAsString, '#\.(yml|yaml)(\'|")$#')) {
return true;
}
// is probably a file variable
if (Strings::match($possibleFileNodeAsString, '#\File$#')) {
return true;
}
// try to detect current value
/** @var Scope $nodeScope */
$nodeScope = $possibleFileNode->getAttribute(Attribute::SCOPE);
$nodeType = $nodeScope->getType($possibleFileNode);
if ($nodeType instanceof ConstantStringType) {
if (Strings::match($nodeType->getValue(), '#\.(yml|yaml)$#')) {
return true;
}
}
return false;
}
}

View File

@ -7,6 +7,3 @@ $parsedFile = Yaml::parse(file_get_contents('someFile.yaml'));
$parsedFile = Yaml::parse(file_get_contents("someFile.yaml"));
$parsedFile = Yaml::parse(file_get_contents($directory . "someFile.yaml"));
$parsedFile = Yaml::parse(file_get_contents(__DIR__ . 'someFile.yaml'));
$someFile = __DIR__ . 'someFile.yaml';
$parsedFile = Yaml::parse($someFile);

View File

@ -0,0 +1,6 @@
<?php declare(strict_types=1);
use Symfony\Component\Yaml\Yaml;
$someFile = __DIR__ . '/someFile.yaml';
$parsedFile = Yaml::parse(file_get_contents($someFile));

View File

@ -0,0 +1,7 @@
<?php declare(strict_types=1);
use Symfony\Component\Yaml\Yaml;
function someFunction($someFile) {
return Yaml::parse(file_get_contents($someFile));
}

View File

@ -21,6 +21,8 @@ final class ParseFileRectorTest extends AbstractRectorTestCase
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
}
protected function provideConfig(): string

View File

@ -7,6 +7,3 @@ $parsedFile = Yaml::parse('someFile.yaml');
$parsedFile = Yaml::parse("someFile.yaml");
$parsedFile = Yaml::parse($directory . "someFile.yaml");
$parsedFile = Yaml::parse(__DIR__ . 'someFile.yaml');
$someFile = __DIR__ . 'someFile.yaml';
$parsedFile = Yaml::parse($someFile);

View File

@ -0,0 +1,6 @@
<?php declare(strict_types=1);
use Symfony\Component\Yaml\Yaml;
$someFile = __DIR__ . '/someFile.yaml';
$parsedFile = Yaml::parse($someFile);

View File

@ -0,0 +1,7 @@
<?php declare(strict_types=1);
use Symfony\Component\Yaml\Yaml;
function someFunction($someFile) {
return Yaml::parse($someFile);
}