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
Do not report serialize as unused #8650
Conversation
Thanks! |
foreach ($serialize_type->getAtomicTypes() as $atomic_serialize_type) { | ||
if ($atomic_serialize_type->isObjectType()) { | ||
return false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it would work for anything but the simplest cases. E.g. serialize([new RuntimeException]);
would still be marked as unused, wouldn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to avoid the error for
String[] but didnt think about object[] indeed.
Do you see an easy way to improve this or should we test for lot of atomic types ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, serialize()
is impure when it operates on anything that may contain an object. Your PRs are focusing on the cases when serialize()
is called on something that does contain an object. The distinction is important because there are supertypes to object types, such as object|int
or iterable
or mixed
. In all of those cases serialize()
should be considered impure:
function a(mixed $p): void { serialize($p); } // impure as an object may be passed here
a(new stdClass);
function b(iterable $p): void { serialize($p); } // ditto
b(new class implements IteratorAggregate {
public function __serialize() { throw new RuntimeException(); }
public function getIterator() { return new ArrayIterator([]); }
});
However, that's only a part of the problem. The other part is that the types that may contain objects could themselves be a part of an arbitrarily deep type. Say you fixed the case of serialize([new stdClass]);
, but then you have to fix serialize([[new stdClass]]);
(note the array depth) and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ContainsObjectTypeVisitor is supposed to resolve the array depth issue, I can add a test about it
For the "may contain an object" issue, I'm not sure how I should change my
if ($type instanceof TObject
|| $type instanceof TNamedObject
|| ($type instanceof TTemplateParam
&& $type->as->hasObjectType())
) {
check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried and didn't succeed to use this function because it doesn't work for arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type visitor approach is fine, you can just use TypeComparator
(either Union*
or Atomic*
, depending on what the visitor gets) to compare individual type parts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way, you'd be able to handle iterable[][]
, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the PR, it works with the previous test but still doesn't work with mixed or iterable.
Did I mess up with the check:
$type instanceof Union
&& UnionTypeComparator::canBeContainedBy($this->codebase, $type, new Union([new TObject()]))
||
$type instanceof Atomic
&& AtomicTypeComparator::isContainedBy($this->codebase, $type, new TObject())
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think the types should be the other way around. The container is the $type
in this case, and the $input_type
is object
, because you want to check whether the object
can be contained by the $type
.
You may have to allow coercion (not sure if that method allows it, but there should be one that does) because object
is not strictly contained by, e.g., ClassName|int
, but it can be coerced into.
Closes #8646