revert [Experimental] Add safe_types option - #3535 (#4120)

This commit is contained in:
Tomas Votruba 2020-09-03 20:02:07 +02:00 committed by GitHub
parent 3da57841db
commit a127bf16eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 0 additions and 219 deletions

View File

@ -262,49 +262,6 @@ vendor/bin/rector process src --set solid --only Rector\SOLID\Rector\Class_\Fina
vendor/bin/rector process src --set solid --only FinalizeClassesWithoutChildrenRector
```
### Safe Types
**Experimental** feature
In default type resolving settings, all docblocks are taken seriously.
```php
<?php
// rector.php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
// [default: false]
$parameters->set(Option::SAFE_TYPES, true);
};
```
E.g. the `TypedPropertyRector` rule will skip this case, as `string` is defined only in docblock:
```php
<?php
class ValueObject
{
public $value;
/**
* @param string $value
*/
public function __construct($value)
{
$this->value = $value;
}
}
```
### Limit Execution to Changed Files
Execution can be limited to changed files using the `process` option `--match-git-diff`.

View File

@ -29,5 +29,4 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$parameters->set(Option::PROJECT_TYPE, Option::PROJECT_TYPE_PROPRIETARY);
$parameters->set(Option::NESTED_CHAIN_METHOD_CALL_LIMIT, 30);
$parameters->set(Option::SAFE_TYPES, false);
};

View File

@ -11,7 +11,6 @@ use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PHPStan\AnalysedCodeException;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
@ -21,13 +20,11 @@ use PHPStan\Reflection\ReflectionProvider;
use Rector\Caching\ChangedFilesDetector;
use Rector\Caching\FileSystem\DependencyResolver;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Configuration\Option;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Collector\TraitNodeScopeCollector;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
@ -86,17 +83,11 @@ final class PHPStanNodeScopeResolver
*/
private $symfonyStyle;
/**
* @var ParameterProvider
*/
private $parameterProvider;
public function __construct(
ChangedFilesDetector $changedFilesDetector,
Configuration $configuration,
DependencyResolver $dependencyResolver,
NodeScopeResolver $nodeScopeResolver,
ParameterProvider $parameterProvider,
ReflectionProvider $reflectionProvider,
RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor,
ScopeFactory $scopeFactory,
@ -112,7 +103,6 @@ final class PHPStanNodeScopeResolver
$this->changedFilesDetector = $changedFilesDetector;
$this->configuration = $configuration;
$this->symfonyStyle = $symfonyStyle;
$this->parameterProvider = $parameterProvider;
}
/**
@ -156,11 +146,6 @@ final class PHPStanNodeScopeResolver
$this->resolveDependentFiles($node, $scope);
};
$safeTypes = (bool) $this->parameterProvider->provideParameter(Option::SAFE_TYPES);
if ($safeTypes) {
$this->removeCommentsFromNodes($nodes);
}
/** @var MutatingScope $scope */
$this->nodeScopeResolver->processNodes($nodes, $scope, $nodeCallback);
@ -212,25 +197,6 @@ final class PHPStanNodeScopeResolver
}
}
/**
* Remove comments, to enable scope resolving only from code, not docblocks
*
* @param Node[] $nodes
*/
private function removeCommentsFromNodes(array $nodes): void
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new class() extends NodeVisitorAbstract {
public function enterNode(Node $node): ?Node
{
$node->setAttribute('comments', null);
return $node;
}
});
$nodeTraverser->traverse($nodes);
}
/**
* @param string[] $dependentFiles
*/

View File

@ -1,31 +0,0 @@
<?php
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\FixtureSafeTypes;
final class CompleteStrictType
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
?>
-----
<?php
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\FixtureSafeTypes;
final class CompleteStrictType
{
private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
?>

View File

@ -1,49 +0,0 @@
<?php
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\FixtureSafeTypes;
final class MakeSureDocsIsKept
{
private $name;
/**
* @var string
*/
private $surname;
/**
* @param string $surname
*/
public function __construct(string $name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
}
?>
-----
<?php
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\FixtureSafeTypes;
final class MakeSureDocsIsKept
{
private string $name;
/**
* @var string
*/
private $surname;
/**
* @param string $surname
*/
public function __construct(string $name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
}
?>

View File

@ -1,16 +0,0 @@
<?php
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector\FixtureSafeTypes;
final class SkipForDoc
{
private $name;
/**
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}
}

View File

@ -1,40 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Php74\Tests\Rector\Property\TypedPropertyRector;
use Iterator;
use Rector\Core\Configuration\Option;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Symplify\SmartFileSystem\SmartFileInfo;
final class SafeTypesTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::SAFE_TYPES, true);
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureSafeTypes');
}
/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
{
return [
TypedPropertyRector::class => [
TypedPropertyRector::CLASS_LIKE_TYPE_ONLY => false,
],
];
}
}

View File

@ -46,11 +46,6 @@ final class Option
*/
public const AUTO_IMPORT_NAMES = 'auto_import_names';
/**
* @var string
*/
public const SAFE_TYPES = 'safe_types';
/**
* @var string
*/