Automattic / vip-go-ci

Continuous integration for VIP Go repositories
Other
50 stars 21 forks source link

Bump VariableAnalysis to 2.9.0 to allow use of the backported options #120

Closed GaryJones closed 3 years ago

GaryJones commented 4 years ago

What problem would the enhancement address for VIP?

When template partials are require'd from within a class, then the partial may use $this->... to insert display a value. At PHP runtime, it works, but for PHPCS analysis, it sees a PHP file with an undefined $this and so reports it via the VariableAnalysis package. Technically it's correct, and while it could be ignored, it's not practical or desirable to do that.

Describe the solution you'd like

VariableAnalysis 2.9.0 includes two new options that can control whether these violations are raised.

VIPCS already requires ^2.8.3, so the code analysis bot could simply pull in the updated version of VA without changes to VIPCS.

Both options could then be set in the config when looking at code.

What code should be reported as a violation?

What code should not be reported as a violation?

Don't report on $this:

<?php
// Example foo.php template.

if ( ! empty( $this->vars['video-id'] ) ) {
    echo '<video...></video>';
}

Don't report on $args (new feature in WP 5.5):

<?php
// Example foo.php template.

// Set defaults.
$args = wp_parse_args(
    $args,
    array(
        'class'          => '',
        'arbitrary_data' => array(
            'foo' => 'fooval',
            'bar' => false,
        ),
        ...
    )
);
?>

<div class="widget <?php echo esc_html_class( $args['class'] ); ?>">
    <?php echo esc_html( $args['arbitrary_data']['foo'] ); ?>
</div>
gudmdharalds commented 4 years ago

@GaryJones: Are the options allowUnusedVariablesBeforeRequire and allowUndefinedVariablesInFileScope? I guess they should be set to true via PHPCS' --runtime-set?

GaryJones commented 4 years ago

@sirbrillig can confirm the option names, but they look right to me.

If you don't have the config in a config file, then yes, --runtime-set can be used.

sirbrillig commented 4 years ago

You can see the full list of options available here: https://github.com/sirbrillig/phpcs-variable-analysis/tree/v2.9.0#customization

gudmdharalds commented 4 years ago

@GaryJones: It seems that this cannot be done via --runtime-set. The documentation says:

To set these these options, you must use XML in your ruleset.

And using these options via --runtime-set has not worked for me:

phpcs --severity='1' --standard='WordPress-VIP-Go' --runtime-set 'allowUnusedVariablesBeforeRequire' 'true' --runtime-set 'allowUndefinedVariablesInFileScope' 'true' /tmp/file-10.php

as it results in:

FILE: /tmp/file-10.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------
 6 | WARNING | Variable $args is undefined.
----------------------------------------------------------------------

I did try a number of variations.

When I tried adding this to the ruleset.xml for WordPress-VIP-Go, it worked instantly:

        <rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
                <properties>
                        <property name="allowUnusedVariablesBeforeRequire" value="true"/>
                        <property name="allowUndefinedVariablesInFileScope" value="true"/>
                </properties>
        </rule>

php /home/teamcity-buildagent/vip-go-ci-tools/phpcs/bin/phpcs --severity='1' --standard='WordPress-VIP-Go' /tmp/testing123/file-10.php

[No output]

I think in order to get this working, we need to add this to the WordPress-VIP-Go ruleset. I can open a PR if needed, just let me know.

gudmdharalds commented 3 years ago

@GaryJones, @rebeccahum: I just wanted to check if this was resolved?

Should I submit a Pull-Request for this? Happy to do so.

GaryJones commented 3 years ago

Go ahead with the PR please :-)

gudmdharalds commented 3 years ago

Resolved here.