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

Custom error messages don't have data replaced when cache is enabled #2575

Closed
ncou opened this issue Aug 3, 2019 · 10 comments
Closed

Custom error messages don't have data replaced when cache is enabled #2575

ncou opened this issue Aug 3, 2019 · 10 comments
Milestone

Comments

@ncou
Copy link

ncou commented Aug 3, 2019

Hi,

The following sniff doesn't seems to work

https://github.com/slevomat/coding-standard/blob/master/build/phpcs-consistence.xml#L202

<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
	<message>@param annotation for parameter "%s" missing</message>
</rule>

The %s string is not replaced with the parameter name. If i remove the custom message it works fine. Is it a regression ? or custom message doesn't support args ?

Thanks for your help.

@jrfnl
Copy link
Contributor

jrfnl commented Aug 4, 2019

@ncou I'm not sure I understand the question. This works perfectly well in my experience.

Just to be sure I ran some tests and found no problems.

This is the code I used for the tests:

<?php

/**
 * Test
 * @return void
 */
function test( $paramA, $paramB ) {
	return;
}

Initial ruleset:

<?xml version="1.0"?>
<ruleset name="Test-2575">
	<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag"/>
</ruleset>

Result:

FILE: /phpcs-2575/test.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
 3 | ERROR | Doc comment for parameter "$paramA" missing
 3 | ERROR | Doc comment for parameter "$paramB" missing
----------------------------------------------------------------------

Ruleset based on your described issue:

<?xml version="1.0"?>
<ruleset name="Test-2575">
	<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
		<message>@param annotation for parameter "%s" missing</message>
	</rule>
</ruleset>

Result:

FILE: /phpcs-2575/test.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
 3 | ERROR | @param annotation for parameter "$paramA" missing
 3 | ERROR | @param annotation for parameter "$paramB" missing
----------------------------------------------------------------------

@ncou
Copy link
Author

ncou commented Aug 4, 2019

here is what i get when i run the test in local (using OS Windows 7 64bits).

<?xml version="1.0"?>
<ruleset name="Coding-Standard">
    <rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
        <message>@param annotation for parameter "%s" missing</message>
    </rule>
</ruleset>

Class :

<?php

class Test
{
    /**
     * Test
     *
     * @return void
     */
    public function test($paramA, $paramB): void
    {
        echo 'test';
    }
}

Result :

FILE: src\Test.php
--------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------
LINE 5: ERROR @param annotation for parameter "%s" missing (Squiz.Commenting.FunctionComment.MissingParamTag)
LINE 5: ERROR @param annotation for parameter "%s" missing (Squiz.Commenting.FunctionComment.MissingParamTag)
--------------------------------------------------------------------------------------------------------------
   3:  class Test
   4:  {
>> 5:      /**
   6:       * Test
   7:       *
--------------------------------------------------------------------------------------------------------------
Time: 111ms; Memory: 4MB

@ncou
Copy link
Author

ncou commented Aug 4, 2019

in my installed.json file it's indicated the php code sniffer version is 3.4.2.

@jrfnl
Copy link
Contributor

jrfnl commented Aug 4, 2019

@ncou How are you running PHPCS ? How is PHPCS installed ?

I honestly cannot reproduce your issue, no matter what I try (and I'm on Windows 7 too).

@gsherwood
Copy link
Member

I can replicate this. It's a problem with caching. When re-adding the messages after loading from cache, the data isn't being replaced. I still need to look into where the problem is coming from.

If you use --no-cache you should see the correct error message, if you are experiencing the same problem.

@gsherwood
Copy link
Member

Actually, I know exactly what is happening.

When errors are replayed after loading from cache, LocalFile::replayErrors() calls File::addMessage() to put the error data back into the file without actually checking it. The addMessage() method looks at the ruleset and finds a custom message format using %s, so it overrides the message with the one from the ruleset. But it doesn't have any data to replace (it is only replaying the errors, not supplying any data) and so the %s goes unreplaced.

The solution for this is to allow addMessage to know that error are being replayed and to not go looking for ruleset settings for the message format.

@gsherwood gsherwood added this to the 3.5.0 milestone Aug 5, 2019
@gsherwood gsherwood changed the title Squiz.Commenting.FunctionComment.MissingParamTag bug with custom message Custom error messages don't have data replaced when cache is enabled Aug 5, 2019
gsherwood added a commit that referenced this issue Aug 5, 2019
@gsherwood
Copy link
Member

I've committed a fix for this, which will be in 3.5.0. For now, disabling cache is the only way to work around this. Or remove the custom error message from the ruleset until the fix is released.

@ncou
Copy link
Author

ncou commented Aug 5, 2019

Yes you are right, if i remove the line <arg name="cache" value=".phpcs-cache"/> in my phpcs.xml file the %s is replaced with the parameter value.

  1. When do you think the v3.5.0 will be released ?

  2. I was searching for a sniff to enforce the PSR Naming rule (interface file should have Interface suffix, idem for Trait and for Abstract class the prefix Abstract) but i could'nt find it in your package. Is there such a sniff in you package ? [sorry for using this issue for a question :-)]

@gsherwood
Copy link
Member

  1. When do you think the v3.5.0 will be released ?

I really don't know. There is a very big refactoring project going into that release so it wont be out until that is complete.

  1. I was searching for a sniff to enforce the PSR Naming rule (interface file should have Interface suffix, idem for Trait and for Abstract class the prefix Abstract) but i could'nt find it in your package. Is there such a sniff in you package ?

No, there is no included sniff for this as it isn't part of the PSR1/2/12 standards directly.

@ncou
Copy link
Author

ncou commented Aug 6, 2019

Thank you for your answers. I will disable the cache and wait for the v3.5 :)
Keep up the good work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants