Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about replacing real class type with @param object $x #3750

Merged
merged 1 commit into from
Feb 27, 2020
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Bug fixes:
+ Don't include both `.` and `vendor/x/y/` when initializing Phan configs with settings such as `--init --init-analyze-dir=.` (#3699)
+ Be more consistent about resolving `static` in generators and template types.
+ Infer the iterable value type for `Generator<V>`. It was previously only inferred when there were 2 or more template args in phpdoc.
+ Don't let less specific type signatures such as `@param object $x` override the real type signature of `MyClass $x` (#3749)

Feb 20 2020, Phan 2.5.0
-----------------------
Expand Down
9 changes: 6 additions & 3 deletions src/Phan/Language/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -3350,9 +3350,12 @@ public function isExclusivelyNarrowedFormOrEquivalentTo(

// Test to see if this (or any ancestor types) can cast to the given union type.
$expanded_types = $this_resolved->asExpandedTypes($code_base);
return $expanded_types->canCastToUnionType(
$union_type
);
foreach ($expanded_types->getTypeSet() as $type) {
if ($type->isSubtypeOfAnyTypeInSet($union_type->getTypeSet())) {
return true;
}
}
return false;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Phan/Language/Type/GenericArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -877,4 +877,10 @@ public function convertIntegerKeyArrayToList(): ArrayType
}
return ListType::fromElementType($this->element_type, $this->is_nullable, $this->key_type);
}

public function isSubtypeOf(Type $type): bool
{
// TODO more specific
return $this->canCastToType($type);
}
}
6 changes: 6 additions & 0 deletions tests/files/expected/0863_vaguer_type.php.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%s:3 PhanTypeMismatchDeclaredParam Doc-block of $dom in vaguer_comment contains phpdoc param type object which is incompatible with the param type \DOMDocument declared in the signature
%s:6 PhanUndeclaredMethod Call to undeclared method \DOMDocument::missingMethod
%s:9 PhanTypeMismatchDeclaredParam Doc-block of $ao in base_class contains phpdoc param type \ArrayAccess which is incompatible with the param type \ArrayObject declared in the signature
%s:12 PhanParamTooManyInternal Call with 1 arg(s) to \ArrayObject::count() which only takes 0 arg(s)
%s:15 PhanTypeMismatchDeclaredParam Doc-block of $dom in wrong_comment contains phpdoc param type \stdClass which is incompatible with the param type \DOMDocument declared in the signature
%s:18 PhanUndeclaredMethod Call to undeclared method \DOMDocument::missingMethod
19 changes: 19 additions & 0 deletions tests/files/src/0863_vaguer_type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @param object $dom
*/
function vaguer_comment(DOMDocument $dom) {
$dom->missingMethod('test');
}
/**
* @param ArrayAccess $ao
*/
function base_class(ArrayObject $ao) {
return $ao->count('unexpected');
}
/**
* @param stdClass $dom
*/
function wrong_comment(DOMDocument $dom) {
$dom->missingMethod('test');
}