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

Format-preserving bug for multipes NodeVisitors #400

Closed
TomasVotruba opened this issue Jul 20, 2017 · 6 comments · Fixed by rectorphp/rector#7
Closed

Format-preserving bug for multipes NodeVisitors #400

TomasVotruba opened this issue Jul 20, 2017 · 6 comments · Fixed by rectorphp/rector#7

Comments

@TomasVotruba
Copy link
Contributor

TomasVotruba commented Jul 20, 2017

With format-perserving printer [#344] I came across weird bug.

I have 2 visitor modifying the code:

This is error output from Travis Report:

  • I make property private - doesn't work 👎
  • I add argument to constructor - works 👍
  • I add assignment in constuctor method - 👎
  • declare (strict_types=1);\n is added instead of it
-    private $property;\n
+    public $property;\n
     /**\n
      * @var DateTimeInterface\n
      */\n
-    private $otherProperty;\n
+    public $otherProperty;\n
     public function __construct(stdClass $property, DateTimeInterface $otherProperty)\n
     {\n
-        $this->property = $property;\n
-        $this->otherProperty = $otherProperty;\n
+        declare (strict_types=1);\n
+        declare (strict_types=1);\n
     }\n
 }\n
return $this->codeStyledPrinter->printToString($newStmts, $oldStmts, $oldTokens);

This would be understandable from some point, but...

But when I switch first 2 arguments, this NodeVisitor works but the other doesn't.

return $this->codeStyledPrinter->printToString($oldStmts, $newStmts, $oldTokens);

In this case, afterTraverse() isn't taken into account.

 class ClassWithNamedService extends Controller\n
 {\n
-    /**\n
-     * @var stdClass\n
-     */\n
-    private $stdClass;\n
-    public function __construct(stdClass $stdClass)\n
-    {\n
-        $this->stdClass = $stdClass;\n
-    }\n
     public function render()\n
     {\n
         $this->stdClass->render();\n
     }\n
 }\n
@TomasVotruba TomasVotruba changed the title Format-preserving bug for multipes nodes Format-preserving bug for multipes NodeVisitors Jul 20, 2017
@TomasVotruba
Copy link
Contributor Author

I used this as hotfix for time being: rectorphp/rector@9ab6c84

@nikic
Copy link
Owner

nikic commented Jul 20, 2017

Where is the CloningVisitor being applied? Unfortunately GH can only search master.

@TomasVotruba
Copy link
Contributor Author

TomasVotruba commented Jul 21, 2017

@TomasVotruba
Copy link
Contributor Author

Does CloningVisitor order matter?

I tried using it fast and using it last and still there are some issues.

@nikic
Copy link
Owner

nikic commented Aug 8, 2017

@TomasVotruba It should definitely run first. To be on the safe side (especially if enterNode visitors are used), I'd also run it as an independent pass instead of interleaving it with other visitors.

@TomasVotruba
Copy link
Contributor Author

TomasVotruba commented Aug 15, 2017

I tried your solution and it works like a charm 🍾

$cloningNodeTraverser = new PhpParser\NodeTraverser();
$cloningNodeTraverser->addVisitor(new PhpParser\NodeVisitor\CloningVisitor);

$oldStmts = $this->parser->parse($fileContent);
$oldTokens = $this->lexer->getTokens();
$newStmts = $cloningNodeTraverser->traverse($oldStmts);
        
$newStmts = $this->nodeTraverser->traverse($newStmts);

$prettyPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens);

Thank you!

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

Successfully merging a pull request may close this issue.

2 participants