-
Notifications
You must be signed in to change notification settings - Fork 145
[TASK] Add native type declarations for CSSBlockList
#1183
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
Conversation
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 think it might be best to first fix the type hints for DeclarationBlock::$selectors
and DeclarationBlock::getSelectors()
.
src/CSSList/CSSBlockList.php
Outdated
} else { | ||
// Non-List `Value` or `CSSString` (CSS identifier) | ||
$result[] = $element; | ||
if ($element instanceof Value) { |
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.
Could this just be an elseif
?
src/CSSList/CSSBlockList.php
Outdated
@@ -149,7 +146,7 @@ protected function allSelectors(array &$result, $specificitySearch = null): void | |||
$comparatorMatched = $selectorSpecificity === $targetSpecificity; | |||
break; | |||
} | |||
if ($comparatorMatched) { | |||
if ($comparatorMatched && $selector instanceof Selector) { |
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.
If we did want to do the instanceof
check, we should do it before calling getSpecificity()
. However, I think it's unnecessary because DeclarationBlock::getSelectors()
always returns an array of Selector
s - and the types in the PHPDoc for DeclarationBlock::$selectors
are incorrect. If we first fix the type hints for that property and its getter, I expect we'd find the instanceof
test is not needed and PHPStan would be happy without 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 took a quick peek into that - that's quite a can of worms, involving covering setSelectors
with tests, refactoring it to no longer temporarily storing strings in DeclarationBlock::selectors
, and more. I'll have a look look at that later and create a ticket.
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.
Ah, yes, I see: during execution of setSelectors()
, the elements in $selectors
may temporarily become strings. And if an exception is thrown, they'll be left in that state. So it's not as straightforward as I thought. When you create the ticket, there should be a note to remove the instanceof
check added here. For now, it can be added, but needs to be before the getSpecificity
method call.
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 have added the first (smaller) part for this with #1184.
Also add some more type checks to ensure that the corresponding types are actually returned. Part of #811
1002e67
to
558831c
Compare
Also add some more type checks to ensure that the corresponding types are actually returned.
Part of #811