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

minor tweaks to the style engine #43111

Merged
merged 1 commit into from
Aug 10, 2022
Merged

minor tweaks to the style engine #43111

merged 1 commit into from
Aug 10, 2022

Conversation

aristath
Copy link
Member

What?

Did an extra review of the style-engine code and added some tweaks to improve our code a bit.

Why?

Because code is poetry.

Comment on lines -37 to -39
if ( empty( $declarations ) ) {
return;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

If empty, then add_declarations won't add anything. This check doesn't serve any purpose.

Comment on lines -97 to +99
* @param array $declarations An array of properties.
* @param array $properties An array of properties.
*
* @return void
*/
public function remove_declarations( $declarations = array() ) {
foreach ( $declarations as $property ) {
public function remove_declarations( $properties = array() ) {
foreach ( $properties as $property ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Simply renamed $declarations to $properties since it's more accurate.

Comment on lines -126 to +123
if ( isset( $property ) && isset( $value ) ) {
return safecss_filter_attr( "{$property}:{$spacer}{$value}" );
}
return '';
return safecss_filter_attr( "{$property}:{$spacer}{$value}" );
Copy link
Member Author

Choose a reason for hiding this comment

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

$property and $value will always be set, they are mandatory params of the method.

Copy link
Member

Choose a reason for hiding this comment

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

Are there checks somewhere else?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes! We check the property and value when adding them to the object, so we already know they're there 😉

Comment on lines +140 to -148
$spacer = $should_prettify ? ' ' : '';

foreach ( $declarations_array as $property => $value ) {
$spacer = $should_prettify ? ' ' : '';
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved this outside the loop. The $spacer's value doesn't change on each iteration of the loop so it can be defined outside it.

Comment on lines -54 to -61
* @return WP_Style_Engine_CSS_Rule|void Returns the object to allow chaining of methods.
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
*/
public function set_selector( $selector ) {
if ( empty( $selector ) ) {
return;
}
$this->selector = $selector;

Copy link
Member Author

Choose a reason for hiding this comment

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

Always return the object to avoid fatalities if chaining methods.

@@ -73,7 +73,7 @@ public function add_rules( $css_rules ) {
public function get_css( $options = array() ) {
$defaults = array(
'optimize' => true,
'prettify' => false,
'prettify' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
Copy link
Member Author

Choose a reason for hiding this comment

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

Use SCRIPT_DEBUG to determine the value of prettify by default.

if ( empty( $style_value ) ) {
return false;
}

return true;
return ! empty( $style_value );
Copy link
Member Author

Choose a reason for hiding this comment

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

Just simplifying code a bit

Comment on lines -515 to +511
return $processor->get_css( array( 'prettify' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) );
return $processor->get_css();
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved this check to the get_css() method itself

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@aristath aristath merged commit a86f875 into trunk Aug 10, 2022
@aristath aristath deleted the try/style-engine-tweaks branch August 10, 2022 08:58
@github-actions github-actions bot added this to the Gutenberg 14.0 milestone Aug 10, 2022
@ramonjd ramonjd added [Package] Style Engine /packages/style-engine [Type] Code Quality Issues or PRs that relate to code quality labels Aug 10, 2022
@andrewserong
Copy link
Contributor

Nice one, thanks for tidying up @aristath!

andrewserong pushed a commit that referenced this pull request Aug 15, 2022
@andrewserong andrewserong added the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label Aug 15, 2022
@andrewserong
Copy link
Contributor

Cherry-picked into the 13.9.0 RC 3 (https://github.com/WordPress/gutenberg/releases/tag/v13.9.0-rc.3)

@andrewserong andrewserong removed the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label Aug 15, 2022
@aristath aristath mentioned this pull request Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Style Engine /packages/style-engine [Type] Code Quality Issues or PRs that relate to code quality
Projects
Status: 🏆 Done
Development

Successfully merging this pull request may close these issues.

4 participants