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

Namespace not tokenized correctly when followed by multiple use statements #648

Closed
xgendrea opened this issue Jul 26, 2015 · 3 comments
Closed

Comments

@xgendrea
Copy link

Hi,

When I launch PHP_CodeSniffer (v2.3.3 ) on this code :

<?php
/**
 * Sending email
 */
namespace Papagei\Scheduler {

    use \Papagei\Record;
    use \Papagei\Debug;

    /**
     * Sending reset email function
     * @version 1.0.0
     * @author unknown
     * @param Record $record Record
     * @return bool
     */
    function reset_mail(Record $record) {         // <--- Line 17
        return true;
    }

}

I have these messages :

----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 17 | ERROR | [x] Line indented incorrectly; expected 0 spaces, found
    |       |     4 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 17 | ERROR | [x] Opening brace must be the last content on the line
    |       |     (Squiz.Functions.MultiLineFunctionDeclaration.ContentAfterBrace)
 21 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found
    |       |     0 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 21 | ERROR | [x] Closing brace indented incorrectly; expected 4 spaces,
    |       |     found 0 (Squiz.WhiteSpace.ScopeClosingBrace.Indent)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

This could be ok but with this code :

<?php
/**
 * Sending email
 */
namespace Papagei\Scheduler {

    /**
     * Sending reset email function
     * @version 1.0.0
     * @author unknown
     * @param mixed $record Record
     * @return bool
     */
    function reset_mail($record) {
        return true;
    }

}

I have no error. This behaviour seems to be strange : Without "use" : no error, with "use" Scope Indent error.

My phpcs.xml is :

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="PAPAGEI">
    <description>PAPAGEI coding standard</description>

    <file>reset_mail.php</file>

    <encoding>utf-8</encoding>

    <arg name="report" value="full"/>
    <arg value="spe"/>

    <rule ref="PSR2">
        <exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/>
        <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine"/>
        <exclude name="Generic.Files.LineLength.TooLong"/>
    </rule>
</ruleset> 

Is it a bug of PHP_CodeSniffer ?

@aik099
Copy link
Contributor

aik099 commented Jul 29, 2015

Maybe the namespace token isn't considered as scope changing token.

@gsherwood
Copy link
Member

Without the use statements, the namespace token is seen as a scope opener. With them, it isn't. No idea why yet.

So this is actually a core tokenizer error rather than something wrong with the scope indent sniff.

@aik099
Copy link
Contributor

aik099 commented Aug 19, 2015

There are 2 use ... constructs:

  1. use ... on top of a file - import a class/interface/trait
  2. use ... inside a class - include a trait in a class

If they are tokenized using same T_USE token (and I guess it is the case), then PHP_CodeSniffer assumption that T_USE token is a scope opener is wrong in 2nd use case (where trait is included into a class).

Interesting part is if namespace Papagei\Scheduler; is used instead of namespace Papagei\Scheduler { then no scope error happens.

@gsherwood gsherwood changed the title Scope indent strange behaviour Namespace not tokenized correctly when followed by multiple use statements Aug 20, 2015
gsherwood added a commit that referenced this issue Aug 20, 2015
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