Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/NodeTypeResolver/src/ComplexNodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ public function resolvePropertyTypeInfo(Property $property): ?VarTypeInfo

$types = array_filter($types);

return new VarTypeInfo($types, $this->typeAnalyzer, $types, true);
return new VarTypeInfo($types, $this->typeAnalyzer, $types);
}
}
60 changes: 30 additions & 30 deletions packages/NodeTypeResolver/src/Php/AbstractTypeInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,17 @@ abstract class AbstractTypeInfo
* @param string[] $types
* @param string[] $fqnTypes
*/
public function __construct(
array $types,
TypeAnalyzer $typeAnalyzer,
array $fqnTypes = [],
bool $allowTypedArrays = false
) {
public function __construct(array $types, TypeAnalyzer $typeAnalyzer, array $fqnTypes = [])
{
$this->typeAnalyzer = $typeAnalyzer;
$this->types = $this->analyzeAndNormalizeTypes($types, $allowTypedArrays);
$this->types = $this->analyzeAndNormalizeTypes($types);

// fallback
if ($fqnTypes === []) {
$fqnTypes = $types;
}

$this->fqnTypes = $this->analyzeAndNormalizeTypes($fqnTypes, $allowTypedArrays);
$this->fqnTypes = $this->analyzeAndNormalizeTypes($fqnTypes);
}

public function isNullable(): bool
Expand Down Expand Up @@ -165,11 +161,35 @@ protected function normalizeName(string $name): string
return ltrim($name, '$');
}

/**
* @param string[] $types
*/
protected function isArraySubtype(array $types): bool
{
if ($types === []) {
return false;
}

foreach ($types as $type) {
if (in_array($type, ['array', 'iterable'], true)) {
continue;
}

if (Strings::endsWith($type, '[]')) {
continue;
}

return false;
}

return true;
}

/**
* @param string|string[] $types
* @return string[]
*/
private function analyzeAndNormalizeTypes($types, bool $allowTypedArrays = false): array
private function analyzeAndNormalizeTypes($types): array
{
$types = (array) $types;

Expand Down Expand Up @@ -207,7 +227,7 @@ private function analyzeAndNormalizeTypes($types, bool $allowTypedArrays = false
continue;
}

$types[$i] = $this->typeAnalyzer->normalizeType($type, $allowTypedArrays);
$types[$i] = $this->typeAnalyzer->normalizeType($type);
}

// remove undesired types
Expand All @@ -226,26 +246,6 @@ private function hasRemovedTypes(): bool
return count($this->removedTypes) > 1;
}

/**
* @param string[] $types
*/
private function isArraySubtype(array $types): bool
{
foreach ($types as $type) {
if (in_array($type, ['array', 'iterable'], true)) {
continue;
}

if (Strings::endsWith($type, '[]')) {
continue;
}

return false;
}

return true;
}

private function normalizeNullable(string $type): string
{
if (Strings::startsWith($type, '?')) {
Expand Down
12 changes: 1 addition & 11 deletions packages/NodeTypeResolver/src/Php/VarTypeInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,6 @@ public function getFqnType(): ?string

public function isIterable(): bool
{
if ($this->types === []) {
return false;
}

foreach ($this->types as $type) {
if ($type !== 'array') {
return false;
}
}

return true;
return $this->isArraySubtype($this->types);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function getReturnTypeInfo(Node $node): ?ReturnTypeInfo

$fqnTypes = $phpDocInfo->getReturnTypes();

return new ReturnTypeInfo($types, $this->typeAnalyzer, $fqnTypes, true);
return new ReturnTypeInfo($types, $this->typeAnalyzer, $fqnTypes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public function __construct(FunctionLikeManipulator $functionLikeManipulator)
public function inferFunctionLike(FunctionLike $functionLike): array
{
$resolvedReturnTypeInfo = $this->functionLikeManipulator->resolveStaticReturnTypeInfo($functionLike);
if ($resolvedReturnTypeInfo === null) {
return [];
}

return $resolvedReturnTypeInfo->getDocTypes();
return $resolvedReturnTypeInfo ? $resolvedReturnTypeInfo->getDocTypes() : [];
}
}
9 changes: 1 addition & 8 deletions src/Php/TypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,8 @@ public function isPhpReservedType(string $type): bool
return false;
}

public function normalizeType(string $type, bool $allowTypedArrays = false): string
public function normalizeType(string $type): string
{
// reduction needed for typehint
if (! $allowTypedArrays) {
if (Strings::endsWith($type, '[]')) {
return 'array';
}
}

if (strtolower($type) === 'boolean') {
return 'bool';
}
Expand Down
6 changes: 3 additions & 3 deletions src/PhpParser/Node/Manipulator/FunctionLikeManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function resolveStaticReturnTypeInfo(FunctionLike $functionLike): ?Return
if (! $functionLike->returnType->getAttribute(
AbstractTypeDeclarationRector::HAS_NEW_INHERITED_TYPE
) && $types !== []) {
return new ReturnTypeInfo($types, $this->typeAnalyzer, $types, true);
return new ReturnTypeInfo($types, $this->typeAnalyzer, $types);
}
}

Expand All @@ -117,12 +117,12 @@ public function resolveStaticReturnTypeInfo(FunctionLike $functionLike): ?Return
}

if ($this->isVoid) {
return new ReturnTypeInfo(['void'], $this->typeAnalyzer, ['void'], true);
return new ReturnTypeInfo(['void'], $this->typeAnalyzer, ['void']);
}

$types = array_filter($types);

return new ReturnTypeInfo($types, $this->typeAnalyzer, $types, true);
return new ReturnTypeInfo($types, $this->typeAnalyzer, $types);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/ClassMethod/AddReturnTypeDeclarationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function processClassMethodNodeWithTypehints(ClassMethod $classMethod, s
if ($newType === '') {
$classMethod->returnType = null;
} else {
$returnTypeInfo = new ReturnTypeInfo([$newType], $this->typeAnalyzer);
$returnTypeInfo = new ReturnTypeInfo([$newType], $this->typeAnalyzer, [$newType]);
$classMethod->returnType = $returnTypeInfo->getFqnTypeNode();
}

Expand Down