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

Anonymous class not tokenized correctly when used as argument to another anon class #2297

Closed
michalbundyra opened this issue Dec 8, 2018 · 3 comments · Fixed by #2299
Closed
Milestone

Comments

@michalbundyra
Copy link
Contributor

Code example:

<?php

new class(
    new class implements Countable {
    }
) extends DateTime {
};

is tokenized as:

00: T_OPEN_TAG => <?php\n
01: T_WHITESPACE => \n
02: T_NEW => new
03: T_WHITESPACE => ·
04: T_CLASS => class            // <!-- should be anonymous class
05: T_OPEN_PARENTHESIS => (
06: T_WHITESPACE => \n
07: T_WHITESPACE => ····
08: T_NEW => new
09: T_WHITESPACE => ·
10: T_ANON_CLASS => class
11: T_WHITESPACE => ·
12: T_IMPLEMENTS => implements
13: T_WHITESPACE => ·
14: T_STRING => Countable
15: T_WHITESPACE => ·
16: T_OPEN_CURLY_BRACKET => {
17: T_WHITESPACE => \n
18: T_WHITESPACE => ····
19: T_CLOSE_CURLY_BRACKET => }
20: T_WHITESPACE => \n
21: T_CLOSE_PARENTHESIS => )
22: T_WHITESPACE => ·
23: T_EXTENDS => extends
24: T_WHITESPACE => ·
25: T_STRING => DateTime
26: T_WHITESPACE => ·
27: T_OPEN_CURLY_BRACKET => {
28: T_WHITESPACE => \n
29: T_CLOSE_CURLY_BRACKET => }
30: T_SEMICOLON => ;
31: T_WHITESPACE => \n

With the token also scope_opener and scope_closer are missing, but I guess when tokenizer will recognize this token correctly then everything will be fine.

@michalbundyra
Copy link
Contributor Author

michalbundyra commented Dec 8, 2018

@jrfnl The code I've provided is valid, it was the minimal code example to show the problem. You are adding closure.

See working example:

<?php

$x = new class(
    new class implements \Countable {
        public function count() {
            return 17;
        }
    }
) extends DateTime {
    private $c;
    public function __construct(\Countable $c) {
        $this->c = $c;
    }
    public function c() {
        return $this->c->count();
    }
};

echo $x->c();

https://3v4l.org/84ZjZ

and tokenizer is still wrong:

00: T_OPEN_TAG => <?php\n
01: T_WHITESPACE => \n
02: T_VARIABLE => $x
03: T_WHITESPACE => ·
04: T_EQUAL => =
05: T_WHITESPACE => ·
06: T_NEW => new
07: T_WHITESPACE => ·
08: T_CLASS => class           // <!------- should be anonymous class
09: T_OPEN_PARENTHESIS => (
10: T_WHITESPACE => \n
11: T_WHITESPACE => ····
12: T_NEW => new
13: T_WHITESPACE => ·
14: T_ANON_CLASS => class    // <!-- class passed as parameter is recognised correctly
15: T_WHITESPACE => ·
...

@jrfnl
Copy link
Contributor

jrfnl commented Dec 8, 2018

@webimpress Yes, sorry about that. I realized too late you were trying to pass a new anonymous class as a parameter to another anonymous class.

@jrfnl
Copy link
Contributor

jrfnl commented Dec 8, 2018

I can confirm that the problem is that the first class token does not get a scope_opener assigned to it prior to the processAdditional() method being run.

michalbundyra added a commit to michalbundyra/Squizlabs_PHP_CodeSniffer that referenced this issue Dec 9, 2018
@gsherwood gsherwood added this to the 3.4.0 milestone Dec 10, 2018
@gsherwood gsherwood changed the title Tokenizer issue - anonymous class Anonymous class not tokenized correctly when used as argument to another anon class Dec 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants