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

[CLEANUP] Add DeclarationBlockParser class #1311

Merged
merged 1 commit into from
Sep 17, 2024

Conversation

JakeQZ
Copy link
Contributor

@JakeQZ JakeQZ commented Sep 14, 2024

This is essentially a refactoring to move the parseCssDeclarationsBlock method that was duplicated in both CssInliner and CssToAttributeConverter to a separate class.

As well as eliminating duplicate code/functionality, there are other benefits:

  • The method can be tested independently, which is duly done;
  • The results cache is now shared;
  • The functionality is readily available to other classes, e.g. for the solution to Support to convert CSS Variables? #1276 (as currently proposed).

PHPStan errors from the original code have been resolved as follows:

  • Use Preg::split to wrap preg_split to handle unexpected errors;
  • Check the return value from preg_match precisely with !== 1 instead of !.

Longer-term, we should be able to use functionality from PHP-CSS-Parser for more accurate parsing (e.g. strings containing semicolons), whereupon this class can become a wrapper for that, but currently that functionality is not available in a standalone class.

@JakeQZ JakeQZ added the cleanup label Sep 14, 2024
@JakeQZ JakeQZ added this to the 8.0.0 milestone Sep 14, 2024
@JakeQZ JakeQZ self-assigned this Sep 14, 2024
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 14, 2024

I can only think that coverage has gone down because the code path in which preg_split fails is never followed.

However, it is probably impossible to pass a declaration block that would cause it to fail (and would depend on PHP environment configuration), because the regex is fairly simple.

Maybe I should add a Preg::split wrapper before putting this in...

@JakeQZ JakeQZ marked this pull request as draft September 14, 2024 22:23
@JakeQZ JakeQZ force-pushed the cleanup/parse-declaration-block branch from ed36927 to ea7d2cd Compare September 14, 2024 22:56
@JakeQZ JakeQZ force-pushed the cleanup/parse-declaration-block branch 2 times, most recently from db2db8b to b21eea3 Compare September 15, 2024 23:09
@JakeQZ JakeQZ marked this pull request as ready for review September 15, 2024 23:10
@JakeQZ JakeQZ force-pushed the cleanup/parse-declaration-block branch 8 times, most recently from 8fd1198 to 88406d7 Compare September 16, 2024 00:22
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 16, 2024

Maybe I should add a Preg::split wrapper before putting this in...

This has eliminated an unexercised statement. I also realized that the cache functionality wasn't being tested (I had previously thought to put in a test for that, then forgot).

Now I am pretty sure all statements/constructs in the new class are covered by the test.

But still coverage is down 0.02%.

Note that there is a conditional ?: in which the RHS is never evaluated. But even without that (and allowing the PHPStan error instead), coverage is still down by the same amount.

I think the issue with the Coveralls test is that it is too simplistic. It just looks at the percentage of code units (statements?) that are covered. Here, I've replaced two completely-covered methods with one completely-covered method. Thus the amount of uncovered code, as a percentage, will inevitably increase. The metric that should be used for CI failure should be the actual amount of uncovered code, not the percentage of it as a whole. I would expect similar problems whenever any functionality is removed.

Unless you can spot any peice of code in the new class that is not covered by tests, I suggest that the Coveralls CI failure be overridden in this case. And we should see if there is an alternative metric that can be used for CI failure (as suggested above), and if not create a ticket.

Obviously review the code as usual :)

@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 16, 2024

Changing to draft again, as now dependent on #1313.

Though the coverage metrics issue described above is still active - @oliverklee?

@JakeQZ JakeQZ marked this pull request as draft September 16, 2024 01:59
@oliverklee
Copy link
Contributor

Unless you can spot any peice of code in the new class that is not covered by tests, I suggest that the Coveralls CI failure be overridden in this case.

Yes, I agree.

I honestly don't know how the coverage/coveralls ended up twice in the list of actions. For my other projects, I only have the first of the two checks.

@oliverklee
Copy link
Contributor

I honestly don't know how the coverage/coveralls ended up twice in the list of actions. For my other projects, I only have the first of the two checks.

I've now checked the "leave PR comments" checkbox (which, for some reason) was not checked in the Coveralls configuration. Let's see.

@coveralls
Copy link

coveralls commented Sep 16, 2024

Coverage Status

coverage: 97.301% (-0.02%) from 97.318%
when pulling 56ae6f0 on cleanup/parse-declaration-block
into c05b168 on main.

@oliverklee
Copy link
Contributor

I've also removed access to the Coveralls Pro 3rd party app now.

@JakeQZ JakeQZ force-pushed the cleanup/parse-declaration-block branch 3 times, most recently from 8a2abf0 to 8389ba4 Compare September 16, 2024 18:36
@JakeQZ JakeQZ marked this pull request as ready for review September 16, 2024 18:38
@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 16, 2024

This can be reviewed now.

@JakeQZ
Copy link
Contributor Author

JakeQZ commented Sep 16, 2024

I've now checked the "leave PR comments" checkbox (which, for some reason) was not checked in the Coveralls configuration. Let's see.

Seems to have worked. However, there was no comment made when I made the final repush to this PR branch. That may have been because the PR was still marked as draft at the time (and chaning PR status from 'draft' to 'ready for review' does not trigger the comment).

JakeQZ added a commit that referenced this pull request Sep 16, 2024
This resolves three PHPStan errors.

Instances of calls to `preg_split` in the two (identical)
`parseCssDeclarationsBlock` methods are not updated - those are covered
separately by #1311.
oliverklee pushed a commit that referenced this pull request Sep 17, 2024
This resolves three PHPStan errors.

Instances of calls to `preg_split` in the two (identical)
`parseCssDeclarationsBlock` methods are not updated - those are covered
separately by #1311.
@oliverklee
Copy link
Contributor

That may have been because the PR was still marked as draft at the time (and chaning PR status from 'draft' to 'ready for review' does not trigger the comment).

Yes, that's what I think as well. (The CI for PRs only runs after a push.)

@oliverklee oliverklee force-pushed the cleanup/parse-declaration-block branch from 8389ba4 to 2b34d83 Compare September 17, 2024 10:53
Copy link
Contributor

@oliverklee oliverklee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I like it!

This is essentially a refactoring to move the `parseCssDeclarationsBlock` method
that was duplicated in both `CssInliner` and `CssToAttributeConverter` to a
separate class.

As well as eliminating duplicate code/functionality, there are other benefits:
- The method can be tested independently, which is duly done;
- The results cache is now shared;
- The functionality is readily available to other classes, e.g. for the solution
  to #1276 (as currently proposed).

PHPStan errors from the original code have been resolved as follows:
- Use `Preg::split` to wrap `preg_split` to handle unexpected errors;
- Check the return value from `preg_match` precisely with `!== 1` instead of
  `!`.

Longer-term, we should be able to use functionality from PHP-CSS-Parser for more
accurate parsing (e.g. strings containing semicolons), whereupon this class can
become a wrapper for that, but currently that functionality is not available in
a standalone class.
@oliverklee oliverklee force-pushed the cleanup/parse-declaration-block branch from 2b34d83 to 56ae6f0 Compare September 17, 2024 10:55
@oliverklee oliverklee merged commit 597eaf2 into main Sep 17, 2024
24 checks passed
@oliverklee oliverklee deleted the cleanup/parse-declaration-block branch September 17, 2024 10:57
JakeQZ added a commit that referenced this pull request Sep 19, 2024
This was added during development while `Preg::split` supported
`PREG_SPLIT_OFFSET_CAPTURE` and thus could return an array of arrays, and the
code contained a check for this.  But before #1311 was completed, #1313 was
implemented to avoid the headache of a dynamic return type.  The check was
removed, but not the corresponding comment explaining it.
oliverklee pushed a commit that referenced this pull request Sep 19, 2024
#1321)

This was added during development while `Preg::split` supported
`PREG_SPLIT_OFFSET_CAPTURE` and thus could return an array of arrays, and the
code contained a check for this.  But before #1311 was completed, #1313 was
implemented to avoid the headache of a dynamic return type.  The check was
removed, but not the corresponding comment explaining it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants