Skip to content

Commit 87b1e59

Browse files
author
Tomáš Votruba
authored
Merge 6320be7 into 3cc5c83
2 parents 3cc5c83 + 6320be7 commit 87b1e59

File tree

12 files changed

+87
-14
lines changed

12 files changed

+87
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/temp
12
/example
23
/vendor
34
composer.lock

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ php:
55

66
install:
77
- composer install
8+
# prepare nette/sandbox
9+
- composer create-project nette/sandbox:@dev temp/nette-sandbox
810

911
script:
1012
- vendor/bin/phpunit --coverage-clover coverage.xml
@@ -13,6 +15,8 @@ script:
1315
- composer check-cs
1416
# check with phpstan (defined in composer.json "scripts" section)
1517
- composer phpstan
18+
# try on nette/sandbox
19+
- bin/rector process temp/nette-sandbox --config src/config/level/nette/all.yml
1620

1721
after_script:
1822
# upload coverage to Coveralls.io

bin/rector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
exit($statusCode);
3434
} catch (Throwable $throwable) {
3535
$symfonyStyle = SymfonyStyleFactory::create();
36-
$symfonyStyle->error($throwable->getMessage());
36+
$symfonyStyle->error(sprintf(
37+
'%s in %s on line %d',
38+
$throwable->getMessage(),
39+
$throwable->getFile(),
40+
$throwable->getLine()
41+
));
3742
exit(1);
3843
}

packages/NodeTypeResolver/src/NodeVisitor/NamespaceResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function beforeTraverse(array $nodes): void
3232
public function enterNode(Node $node): void
3333
{
3434
if ($node instanceof Namespace_) {
35-
$this->namespace = $node->name->toString();
35+
$this->namespace = $node->name ? $node->name->toString() : '';
3636
}
3737

3838
if ($node instanceof Use_) {

packages/NodeTypeResolver/src/NodeVisitor/TypeResolver.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,38 @@ public function enterNode(Node $node): void
8787

8888
private function getTypeFromNewNode(New_ $newNode): string
8989
{
90+
// e.g. new $someClass;
9091
if ($newNode->class instanceof Variable) {
9192
// can be anything (dynamic)
9293
$variableName = $newNode->class->name;
9394

9495
return $this->typeContext->getTypeForVariable($variableName);
9596
}
9697

98+
// e.g. new SomeClass;
9799
if ($newNode->class instanceof Name) {
98100
/** @var FullyQualified $fqnName */
99101
$fqnName = $newNode->class->getAttribute(Attribute::RESOLVED_NAME);
100102

101103
return $fqnName->toString();
102104
}
103105

106+
// e.g. new $this->templateClass;
107+
if ($newNode->class instanceof PropertyFetch) {
108+
if ($newNode->class->var->name !== 'this') {
109+
throw new NotImplementedException(sprintf(
110+
'Not implemented yet. Go to "%s()" and add check for "%s" node for external dependency.',
111+
__METHOD__,
112+
get_class($newNode->class)
113+
));
114+
}
115+
116+
// can be anything (dynamic)
117+
$propertyName = (string) $newNode->class->name;
118+
119+
return $this->typeContext->getTypeForProperty($propertyName);
120+
}
121+
104122
throw new NotImplementedException(sprintf(
105123
'Not implemented yet. Go to "%s()" and add check for "%s" node.',
106124
__METHOD__,

packages/NodeTypeResolver/src/TypeContext.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use PhpParser\Node\FunctionLike;
77
use PhpParser\Node\Stmt\Class_;
88
use PhpParser\Node\Stmt\ClassLike;
9+
use PhpParser\Node\Stmt\ClassMethod;
10+
use PhpParser\Node\Stmt\Function_;
911
use Rector\BetterReflection\Reflector\MethodReflector;
1012
use Rector\NodeTypeResolver\TypesExtractor\ConstructorPropertyTypesExtractor;
11-
use ReflectionFunction;
13+
use Roave\BetterReflection\Reflection\ReflectionFunction;
1214
use Roave\BetterReflection\Reflection\ReflectionMethod;
1315

1416
/**
@@ -100,21 +102,29 @@ public function addAssign(string $newVariable, string $oldVariable): void
100102
}
101103

102104
/**
103-
* @return \Roave\BetterReflection\Reflection\ReflectionFunction|ReflectionMethod|null
105+
* @param Function_|ClassMethod|Closure $functionLikeNode
106+
*
107+
* @return ReflectionFunction|ReflectionMethod|null
104108
*/
105109
private function getFunctionReflection(FunctionLike $functionLikeNode)
106110
{
107-
if ($this->classLikeNode) {
108-
if ($functionLikeNode instanceof Closure) {
109-
return null;
110-
}
111+
if ($functionLikeNode instanceof Closure) {
112+
return null;
113+
}
111114

115+
if ($this->classLikeNode) {
112116
$className = $this->classLikeNode->namespacedName->toString();
113117
$methodName = (string) $functionLikeNode->name;
114118

115119
return $this->methodReflector->reflectClassMethod($className, $methodName);
116120
}
117121

118-
return new ReflectionFunction((string) $functionLikeNode->name);
122+
/** @var Function_ $functionLikeNode */
123+
$functionName = (string) $functionLikeNode->name;
124+
if (! function_exists($functionName)) {
125+
return null;
126+
}
127+
128+
return ReflectionFunction::createFromName($functionName);
119129
}
120130
}

rector.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
rectors:
2-
- Rector\Rector\Contrib\Nette\Application\InjectPropertyRector
2+
- Rector\Rector\Contrib\Nette\Application\InjectPropertyRector

src/Console/Command/ProcessCommand.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Nette\Utils\Finder;
66
use Rector\Application\FileProcessor;
7+
use Rector\Exception\FileSystem\DirectoryNotFoundException;
78
use Rector\Exception\NoRectorsLoadedException;
89
use Rector\Naming\CommandNaming;
910
use Rector\Rector\RectorCollector;
@@ -61,15 +62,17 @@ protected function configure(): void
6162

6263
protected function execute(InputInterface $input, OutputInterface $output): int
6364
{
64-
$this->ensureSomeRectorsAreRegistered();
65+
$source = $input->getArgument(self::ARGUMENT_SOURCE_NAME);
66+
$this->ensureDirectoriesExist($source);
6567

66-
$this->reportLoadedRectors();
68+
$this->ensureSomeRectorsAreRegistered();
6769

68-
$source = $input->getArgument(self::ARGUMENT_SOURCE_NAME);
6970
$files = $this->findPhpFilesInDirectories($source);
7071

7172
$this->reportFoundFiles($files);
7273

74+
$this->reportLoadedRectors();
75+
7376
$this->fileProcessor->processFiles($files);
7477

7578
return 0;
@@ -133,4 +136,21 @@ private function reportFoundFiles(array $files): void
133136

134137
$this->symfonyStyle->newLine();
135138
}
139+
140+
/**
141+
* @param string[] $directories
142+
*/
143+
private function ensureDirectoriesExist(array $directories): void
144+
{
145+
foreach ($directories as $directory) {
146+
if (file_exists($directory)) {
147+
continue;
148+
}
149+
150+
throw new DirectoryNotFoundException(sprintf(
151+
'Directory "%s" was not found.',
152+
$directory
153+
));
154+
}
155+
}
136156
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Rector\Exception\FileSystem;
4+
5+
use Exception;
6+
7+
final class DirectoryNotFoundException extends Exception
8+
{
9+
}

src/Rector/Contrib/Nette/Bootstrap/RemoveConfiguratorConstantsRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\ClassConstFetch;
7+
use PhpParser\Node\Expr\Variable;
78
use PhpParser\Node\Scalar\String_;
89
use Rector\Node\Attribute;
910
use Rector\Rector\AbstractRector;
@@ -42,6 +43,10 @@ private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetc
4243
/** @var Node\Name\FullyQualified $fqnName */
4344
$fqnName = $classConstFetchNode->class->getAttribute(Attribute::RESOLVED_NAME);
4445

46+
if ($fqnName === null && $classConstFetchNode->class instanceof Variable) {
47+
return (string) $classConstFetchNode->class->name;
48+
}
49+
4550
return $fqnName->toString();
4651
}
4752

0 commit comments

Comments
 (0)