Skip to content

[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

Merged
merged 2 commits into from
Mar 17, 2025

Conversation

oliverklee
Copy link
Collaborator

Also add some more type checks to ensure that the corresponding types are actually returned.

Part of #811

@coveralls
Copy link

coveralls commented Mar 17, 2025

Coverage Status

coverage: 56.839% (-0.03%) from 56.87%
when pulling 558831c on task/types/cssblocklist
into 1226a2f on main.

Copy link
Collaborator

@JakeQZ JakeQZ left a 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().

Comment on lines 103 to 104
} else {
// Non-List `Value` or `CSSString` (CSS identifier)
$result[] = $element;
if ($element instanceof Value) {
Copy link
Collaborator

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?

@@ -149,7 +146,7 @@ protected function allSelectors(array &$result, $specificitySearch = null): void
$comparatorMatched = $selectorSpecificity === $targetSpecificity;
break;
}
if ($comparatorMatched) {
if ($comparatorMatched && $selector instanceof Selector) {
Copy link
Collaborator

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 Selectors - 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.

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator Author

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
@oliverklee oliverklee force-pushed the task/types/cssblocklist branch from 1002e67 to 558831c Compare March 17, 2025 22:02
@oliverklee oliverklee requested a review from JakeQZ March 17, 2025 22:02
@JakeQZ JakeQZ merged commit a583a2e into main Mar 17, 2025
21 checks passed
@JakeQZ JakeQZ deleted the task/types/cssblocklist branch March 17, 2025 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants