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
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public function refactor(Node $node): ?Node
{
$this->hasChanged = false;

// skip class without parents/interfaces, there is no param type to mimic
if ($node instanceof Class_ && !$node->extends instanceof Name && $node->implements === []) {
return null;
}

// skip guarded classes, where adding a param type would break child classes
if ($this->parentClassMethodTypeOverrideGuard->isTypeGuardedClass($node)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,24 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool

private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod): bool
{
if ($this->classModifierChecker->isInsideFinalClass($classMethod)) {
if (count((array) $classMethod->stmts) !== 1) {
return false;
}

if (count((array) $classMethod->stmts) !== 1) {
$onlyStmt = $classMethod->stmts[0] ?? null;
if (! $onlyStmt instanceof Expression || ! $onlyStmt->expr instanceof Throw_) {
return false;
}

$onlyStmt = $classMethod->stmts[0] ?? null;
return $onlyStmt instanceof Expression && $onlyStmt->expr instanceof Throw_;
return ! $this->classModifierChecker->isInsideFinalClass($classMethod);
}

private function isNotFinalAndEmpty(ClassMethod $classMethod): bool
{
if ($this->classModifierChecker->isInsideFinalClass($classMethod)) {
if ($classMethod->stmts !== []) {
return false;
}

return $classMethod->stmts === [];
return ! $this->classModifierChecker->isInsideFinalClass($classMethod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $this->isNames($classMethod, [
MethodName::CALL,
MethodName::CALL_STATIC,
MethodName::__SET,
MethodName::__GET,
MethodName::ISSET,
MethodName::UNSET,
])) {
continue;
}

if ($this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod)) {
continue;
}
Expand All @@ -91,23 +102,11 @@ public function refactor(Node $node): ?Node
}
}

// first arg string
if ($this->isNames(
$classMethod,
[
MethodName::CALL,
MethodName::CALL_STATIC,
MethodName::__SET,
MethodName::__GET,
MethodName::ISSET,
MethodName::UNSET,
]
)) {
$firstParam = $classMethod->getParams()[0];
if (! $firstParam->type instanceof Node) {
$firstParam->type = new Identifier('string');
$hasChanged = true;
}
// first arg string, shared by all handled magic methods
$firstParam = $classMethod->getParams()[0];
if (! $firstParam->type instanceof Node) {
$firstParam->type = new Identifier('string');
$hasChanged = true;
}

if ($this->isName($classMethod, MethodName::__GET) && $this->phpVersionProvider->isAtLeastPhpVersion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ private function isTestClassMethodWithFilledReturnType(ClassMethod|Function_ $ca
return false;
}

if (! $this->testsNodeAnalyzer->isInTestClass($callLike)) {
if (! $callLike->returnType instanceof Node) {
return false;
}

return $callLike->returnType instanceof Node;
return $this->testsNodeAnalyzer->isInTestClass($callLike);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

// nothing useful here
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
if (! $setUpClassMethod instanceof ClassMethod) {
return null;
}

if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

$hasChanged = false;

foreach ($node->getProperties() as $property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$yieldNodes = $this->yieldNodeFinder->find($node);
if ($yieldNodes === []) {
return null;
}

// skip already filled type
if ($node->returnType instanceof Node && $this->isNames(
$node->returnType,
Expand All @@ -88,6 +83,11 @@ public function refactor(Node $node): ?Node
return null;
}

$yieldNodes = $this->yieldNodeFinder->find($node);
if ($yieldNodes === []) {
return null;
}

$scope = ScopeFetcher::fetch($node);
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod(
$node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public function refactor(Node $node): ?Node
}

foreach ($this->addPropertyTypeDeclarations as $addPropertyTypeDeclaration) {
if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) {
if (! $this->isName($node, $addPropertyTypeDeclaration->getPropertyName())) {
continue;
}

if (! $this->isName($node, $addPropertyTypeDeclaration->getPropertyName())) {
if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) {
continue;
}

Expand Down
Loading