misc compiler

This commit is contained in:
TomasVotruba 2020-01-28 00:45:52 +01:00
parent 38241af7bc
commit 7d53bd5797
3 changed files with 49 additions and 25 deletions

View File

@ -9,6 +9,7 @@
"symfony/process": "^4.4|^5.0",
"symfony/filesystem": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"symplify/package-builder": "^7.2",
"nette/utils": "^3.0"
},
"autoload": {

View File

@ -6,11 +6,15 @@ namespace Rector\Compiler\Console;
use Nette\Utils\FileSystem as NetteFileSystem;
use Nette\Utils\Json;
use Rector\Console\Style\SymfonyStyleFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;
/**
* Inspired by @see https://github.com/phpstan/phpstan-src/blob/f939d23155627b5c2ec6eef36d976dddea22c0c5/compiler/src/Console/CompileCommand.php
@ -37,12 +41,20 @@ final class CompileCommand extends Command
*/
private $originalComposerJsonFileContent;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(string $dataDir, string $buildDir)
{
parent::__construct();
$this->filesystem = new Filesystem();
$this->dataDir = $dataDir;
$this->buildDir = $buildDir;
$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesCaller());
$this->symfonyStyle = $symfonyStyleFactory->create();
}
protected function configure(): void
@ -55,6 +67,8 @@ final class CompileCommand extends Command
{
$composerJsonFile = $this->buildDir . '/composer.json';
$this->symfonyStyle->note('Loading ' . $composerJsonFile);
$this->fixComposerJson($composerJsonFile);
// @see https://github.com/dotherightthing/wpdtrt-plugin-boilerplate/issues/52
@ -79,7 +93,7 @@ final class CompileCommand extends Command
$this->restoreComposerJson($composerJsonFile);
return 0;
return ShellCode::SUCCESS;
}
private function fixComposerJson(string $composerJsonFile): void

View File

@ -7,6 +7,7 @@ namespace Rector\NodeTypeResolver\PerNodeTypeResolver;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\NodeTraverser;
use PHPStan\Type\MixedType;
@ -75,36 +76,20 @@ final class ParamTypeResolver implements PerNodeTypeResolverInterface
*/
public function resolve(Node $node): Type
{
if ($node->type !== null) {
if (! $node->type instanceof Node\Identifier) {
$resolveTypeName = $this->nameResolver->getName($node->type);
if ($resolveTypeName) {
// @todo map the other way every type :)
return new ObjectType($resolveTypeName);
}
}
$paramType = $this->resolveFromType($node);
if (! $paramType instanceof MixedType) {
return $paramType;
}
$resolvedType = $this->resolveParamStaticType($node);
if (! $resolvedType instanceof MixedType) {
return $resolvedType;
$firstVariableUseType = $this->resolveFromFirstVariableUse($node);
if (! $firstVariableUseType instanceof MixedType) {
return $firstVariableUseType;
}
/** @var FunctionLike $parentNode */
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
return $this->resolveTypesFromFunctionDocBlock($node, $parentNode);
return $this->resolveFromFunctionDocBlock($node);
}
private function resolveTypesFromFunctionDocBlock(Param $param, FunctionLike $functionLike): Type
{
/** @var string $paramName */
$paramName = $this->nameResolver->getName($param);
return $this->docBlockManipulator->getParamTypeByName($functionLike, '$' . $paramName);
}
private function resolveParamStaticType(Param $param): Type
private function resolveFromFirstVariableUse(Param $param): Type
{
$classMethod = $param->getAttribute(AttributeKey::METHOD_NODE);
if ($classMethod === null) {
@ -135,4 +120,28 @@ final class ParamTypeResolver implements PerNodeTypeResolverInterface
return $paramStaticType;
}
private function resolveFromFunctionDocBlock(Param $param): Type
{
/** @var FunctionLike $parentNode */
$parentNode = $param->getAttribute(AttributeKey::PARENT_NODE);
/** @var string $paramName */
$paramName = $this->nameResolver->getName($param);
return $this->docBlockManipulator->getParamTypeByName($parentNode, '$' . $paramName);
}
private function resolveFromType(Node $node)
{
if ($node->type !== null && ! $node->type instanceof Identifier) {
$resolveTypeName = $this->nameResolver->getName($node->type);
if ($resolveTypeName) {
// @todo map the other way every type :)
return new ObjectType($resolveTypeName);
}
}
return new MixedType();
}
}