Skip to content

Commit

Permalink
sort methods by their call
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 16, 2018
1 parent 558fa59 commit 3f8c943
Show file tree
Hide file tree
Showing 29 changed files with 378 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ private function shouldSkipForeach(Foreach_ $foreachNode): bool
return ! $foreachNode->stmts[0] instanceof If_;
}

private function shouldSkipIf(If_ $ifNode): bool
{
$ifCondition = $ifNode->cond;
return ! $ifCondition instanceof Identical && ! $ifCondition instanceof Equal;
}

private function isIfBodyABoolReturnNode(If_ $firstNodeInsideForeach): bool
{
$ifStatment = $firstNodeInsideForeach->stmts[0];
Expand Down Expand Up @@ -212,10 +218,4 @@ private function combineCommentsToNode(Node $originalNode, Node $newNode): void

$newNode->setAttribute('comments', [new Comment($commentContent)]);
}

private function shouldSkipIf(If_ $ifNode): bool
{
$ifCondition = $ifNode->cond;
return ! $ifCondition instanceof Identical && ! $ifCondition instanceof Equal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ function (Node $node) {
return $inArrayFuncCall;
}

private function shouldBeStrict(BinaryOp $binaryOpNode): bool
{
return $binaryOpNode instanceof Identical || $binaryOpNode instanceof NotIdentical;
}

private function resolveIsNot(BinaryOp $node, ConstFetch $boolConstFetchNode): bool
{
if ($node instanceof Identical || $node instanceof Equal) {
Expand All @@ -98,9 +103,4 @@ private function resolveIsNot(BinaryOp $node, ConstFetch $boolConstFetchNode): b

return $this->isTrue($boolConstFetchNode);
}

private function shouldBeStrict(BinaryOp $binaryOpNode): bool
{
return $binaryOpNode instanceof Identical || $binaryOpNode instanceof NotIdentical;
}
}
22 changes: 11 additions & 11 deletions packages/NodeTypeResolver/src/NodeTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ public function isCountableType(Node $node): bool
return $nodeType instanceof ArrayType;
}

private function getNodeType(Node $node): ?Type
{
/** @var Scope|null $nodeScope */
$nodeScope = $node->getAttribute(Attribute::SCOPE);
if (! $node instanceof Expr || $nodeScope === null) {
return null;
}

return $nodeScope->getType($node);
}

/**
* Special case for "preg_match(), preg_match_all()" - with 3rd argument
* @covers https://github.com/rectorphp/rector/issues/786
Expand Down Expand Up @@ -134,15 +145,4 @@ private function correctPregMatchType(Node $node, Type $originalType): Type

return new ArrayType(new MixedType(), new MixedType());
}

private function getNodeType(Node $node): ?Type
{
/** @var Scope|null $nodeScope */
$nodeScope = $node->getAttribute(Attribute::SCOPE);
if (! $node instanceof Expr || $nodeScope === null) {
return null;
}

return $nodeScope->getType($node);
}
}
20 changes: 10 additions & 10 deletions packages/NodeTypeResolver/src/PHPStan/Scope/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ function (Node $node, Scope $scope): void {
return $nodes;
}

/**
* @param Node[] $nodes
*/
private function removeDeepChainMethodCallNodes(array $nodes): void
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($this->removeDeepChainMethodCallNodeVisitor);
$nodeTraverser->traverse($nodes);
}

/**
* @param Class_|Interface_ $classOrInterfaceNode
*/
Expand All @@ -100,14 +110,4 @@ private function resolveClassOrInterfaceNode(Node $classOrInterfaceNode, Scope $

return $scope;
}

/**
* @param Node[] $nodes
*/
private function removeDeepChainMethodCallNodes(array $nodes): void
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($this->removeDeepChainMethodCallNodeVisitor);
$nodeTraverser->traverse($nodes);
}
}
90 changes: 45 additions & 45 deletions packages/NodeTypeResolver/src/Php/AbstractTypeInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@
abstract class AbstractTypeInfo
{
/**
* @var string[]
* @var bool
*/
protected $types = [];
protected $isNullable = false;

/**
* @var string[]
* @var bool
*/
protected $typesToRemove = [];
protected $hasRemovedTypes = false;

/**
* @var bool
* @var string[]
*/
protected $isNullable = false;
protected $types = [];

/**
* @var string[]
*/
protected $fqnTypes = [];
protected $typesToRemove = [];

/**
* @var bool
* @var string[]
*/
protected $hasRemovedTypes = false;
protected $fqnTypes = [];

/**
* @var string[]
Expand Down Expand Up @@ -184,24 +184,33 @@ protected function analyzeAndNormalizeTypes($types): array

/**
* @param string[] $types
* @return string[]
*/
private function squashTraversableAndArrayToIterable(array $types): array
private function isArraySubtype(array $types): bool
{
// Traversable | array = iterable
if (count(array_intersect($this->iterableUnionTypes, $types)) !== 2) {
return $types;
$arraySubtypeGroup = ['array', 'iterable'];
return $this->areArraysEqual($types, $arraySubtypeGroup);
}

private function normalizeNullable(string $type): string
{
if (Strings::startsWith($type, '?')) {
$type = ltrim($type, '?');
$this->isNullable = true;
}
return $type;
}

foreach ($types as $i => $type) {
if (in_array($type, $this->iterableUnionTypes, true)) {
unset($types[$i]);
}
private function normalizeCasing(string $type): string
{
if (TypeAnalyzer::isPhpReservedType($type)) {
return strtolower($type);
}

$types[] = 'iterable';
if (strtolower($type) === '$this') {
return strtolower($type);
}

return $types;
return $type;
}

/**
Expand All @@ -226,11 +235,24 @@ private function removeTypes(array $types): array

/**
* @param string[] $types
* @return string[]
*/
private function isArraySubtype(array $types): bool
private function squashTraversableAndArrayToIterable(array $types): array
{
$arraySubtypeGroup = ['array', 'iterable'];
return $this->areArraysEqual($types, $arraySubtypeGroup);
// Traversable | array = iterable
if (count(array_intersect($this->iterableUnionTypes, $types)) !== 2) {
return $types;
}

foreach ($types as $i => $type) {
if (in_array($type, $this->iterableUnionTypes, true)) {
unset($types[$i]);
}
}

$types[] = 'iterable';

return $types;
}

/**
Expand All @@ -244,26 +266,4 @@ private function areArraysEqual(array $types, array $arraySubtypeGroup): bool

return $types === $arraySubtypeGroup;
}

private function normalizeNullable(string $type): string
{
if (Strings::startsWith($type, '?')) {
$type = ltrim($type, '?');
$this->isNullable = true;
}
return $type;
}

private function normalizeCasing(string $type): string
{
if (TypeAnalyzer::isPhpReservedType($type)) {
return strtolower($type);
}

if (strtolower($type) === '$this') {
return strtolower($type);
}

return $type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ public function getVarTypeInfo(Node $node): ?VarTypeInfo
return new VarTypeInfo($types, $fqnTypes);
}

private function createPhpDocInfoWithFqnTypesFromNode(Node $node): PhpDocInfo
{
$phpDocInfo = $this->createPhpDocInfoFromNode($node);

$this->phpDocInfoFqnTypeDecorator->decorate($phpDocInfo, $node);

return $phpDocInfo;
}

private function isNamespaced(string $name): bool
{
return Strings::contains($name, '\\');
}

private function updateNodeWithPhpDocInfo(Node $node, PhpDocInfo $phpDocInfo): void
{
// skip if has no doc comment
Expand Down Expand Up @@ -278,18 +292,4 @@ private function createPhpDocInfoFromNode(Node $node): PhpDocInfo

return $this->phpDocInfoFactory->createFrom($node->getDocComment()->getText());
}

private function createPhpDocInfoWithFqnTypesFromNode(Node $node): PhpDocInfo
{
$phpDocInfo = $this->createPhpDocInfoFromNode($node);

$this->phpDocInfoFqnTypeDecorator->decorate($phpDocInfo, $node);

return $phpDocInfo;
}

private function isNamespaced(string $name): bool
{
return Strings::contains($name, '\\');
}
}
24 changes: 12 additions & 12 deletions packages/Php/src/Rector/Assign/MysqlAssignToMysqliRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public function refactor(Node $node): ?Node
return $this->processFieldToFieldDirect($node, $funcCallNode);
}

private function processMysqlTableName(Assign $assignNode, FuncCall $funcCall): FuncCall
{
$funcCall->name = new Name('mysqli_data_seek');

$newFuncCall = new FuncCall(new Name('mysql_fetch_array'), [$funcCall->args[0]]);
$newAssignNode = new Assign($assignNode->var, new ArrayDimFetch($newFuncCall, new LNumber(0)));

$this->addNodeAfterNode($newAssignNode, $assignNode);

return $funcCall;
}

private function processMysqlDbName(Assign $assignNode, FuncCall $funcCallNode): FuncCall
{
$funcCallNode->name = new Name('mysqli_data_seek');
Expand Down Expand Up @@ -143,18 +155,6 @@ private function processMysqlFetchField(Assign $assignNode, FuncCall $funcCallNo
return $assignNode;
}

private function processMysqlTableName(Assign $assignNode, FuncCall $funcCall): FuncCall
{
$funcCall->name = new Name('mysqli_data_seek');

$newFuncCall = new FuncCall(new Name('mysql_fetch_array'), [$funcCall->args[0]]);
$newAssignNode = new Assign($assignNode->var, new ArrayDimFetch($newFuncCall, new LNumber(0)));

$this->addNodeAfterNode($newAssignNode, $assignNode);

return $funcCall;
}

private function processFieldToFieldDirect(Assign $assignNode, FuncCall $funcCallNode): ?Assign
{
foreach ($this->fieldToFieldDirect as $funcName => $property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ public function refactor(Node $node): ?Node
return $anonymousFunctionNode;
}

/**
* @return Param[]
*/
private function parseStringToParameters(Expr $expr): array
{
$content = $this->stringify($expr);

$content = '<?php $value = function(' . $content . ') {};';

$nodes = $this->parser->parse($content);

return $nodes[0]->expr->expr->params;
}

/**
* @param String_|Expr $content
* @return Stmt[]
Expand All @@ -133,20 +147,6 @@ private function parseStringToBody(Node $content): array
return (array) $this->parser->parse('<?php ' . $content);
}

/**
* @return Param[]
*/
private function parseStringToParameters(Expr $expr): array
{
$content = $this->stringify($expr);

$content = '<?php $value = function(' . $content . ') {};';

$nodes = $this->parser->parse($content);

return $nodes[0]->expr->expr->params;
}

/**
* @param Node[] $nodes
* @param Variable[] $paramNodes
Expand Down
Loading

0 comments on commit 3f8c943

Please sign in to comment.