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

Wrong System-Error location #8755

Closed
llaville opened this issue Jul 25, 2024 · 1 comment · Fixed by rectorphp/rector-src#6184
Closed

Wrong System-Error location #8755

llaville opened this issue Jul 25, 2024 · 1 comment · Fixed by rectorphp/rector-src#6184
Labels

Comments

@llaville
Copy link

Bug Report

Subject Details
Rector version 1.2.1 and recent 1.2.2

Minimal PHP Code Causing Issue

When you try to parse PHP scripts that have any parsing errors, the location in each files is wrong (always give line 94)

For Example

system-error-wrong-location

Expected Behaviour

Give the right line in each file where parsing error is detected !

In above example we should see line 3 and 15.

I've already a PR ready, if you're agree with such fix :

Introduces a ParserErrors class

<?php

namespace Rector\PhpParser\Parser;

use PHPStan\Parser\ParserErrorsException;

final class ParserErrors
{
    private string $message;
    private int $line;

    public function __construct(ParserErrorsException $exception)
    {
        $this->message = $exception->getMessage();
        $this->line = $exception->getAttributes()['startLine'] ?? 0;
    }

    public function getMessage(): string
    {
        return $this->message;
    }

    public function getLine(): int
    {
        return $this->line;
    }
}

And populate it when we detect that Throwable exception is compatible

            if ($throwable instanceof ParserErrorsException) {
                $throwable = new ParserErrors($throwable);
            }

            return new SystemError($throwable->getMessage(), $relativeFilePath, $throwable->getLine());

That will give following results (as expected)

When verbose mode is disabled

For example

bin/rector process --dry-run /shared/backups/bartlett/sarif-php-converters/examples/fixtures/

Prints this output

 2/2 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 [ERROR] Could not process "../../bartlett/sarif-php-converters/examples/fixtures/php_bad_2.php" file, due to:
         "Syntax error, unexpected '}'". On line: 15



 [ERROR] Could not process "../../bartlett/sarif-php-converters/examples/fixtures/php_bad_1.php" file, due to:
         "Syntax error, unexpected T_STRING, Syntax error, unexpected '=', Invalid numeric literal, Syntax error,
         unexpected T_LNUMBER, Invalid numeric literal, Syntax error, unexpected T_STRING". On line: 3


When verbose mode is enabled

For example

bin/rector process --dry-run /shared/backups/bartlett/sarif-php-converters/examples/fixtures/ -v

Prints this output


 [ERROR] Could not process "/shared/backups/bartlett/sarif-php-converters/examples/fixtures/php_bad_1.php" file, due to:
         "System error: "Syntax error, unexpected T_STRING, Syntax error, unexpected '=', Invalid numeric literal,
         Syntax error, unexpected T_LNUMBER, Invalid numeric literal, Syntax error, unexpected T_STRING"
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On line: 3



 [ERROR] Could not process "/shared/backups/bartlett/sarif-php-converters/examples/fixtures/php_bad_2.php" file, due to:
         "System error: "Syntax error, unexpected '}'"
         Run Rector with "--debug" option and post the report here: https://github.com/rectorphp/rector/issues/new". On line: 15

When debug mode is enabled

For example

bin/rector process --dry-run /shared/backups/bartlett/sarif-php-converters/examples/fixtures/ -vvv

Prints this output


 [ERROR] Could not process "/shared/backups/bartlett/sarif-php-converters/examples/fixtures/php_bad_1.php" file, due to:
         "System error: "Syntax error, unexpected T_STRING, Syntax error, unexpected '=', Invalid numeric literal,
         Syntax error, unexpected T_LNUMBER, Invalid numeric literal, Syntax error, unexpected T_STRING"

         Stack trace:
         #0 src/PhpParser/Parser/RectorParser.php(40): PHPStan\Parser\RichParser->parseString('<?php\n\n2pe98y r...')
         #1 src/Application/FileProcessor.php(193):
         Rector\PhpParser\Parser\RectorParser->parseFileContentToStmtsAndTokens('<?php\n\n2pe98y r...')
         #2 src/Application/FileProcessor.php(114):
         Rector\Application\FileProcessor->parseFileNodes(Object(Rector\ValueObject\Application\File))
         #3 src/Application/FileProcessor.php(56):
         Rector\Application\FileProcessor->parseFileAndDecorateNodes(Object(Rector\ValueObject\Application\File))
         #4 src/Application/ApplicationFileProcessor.php(168):
         Rector\Application\FileProcessor->processFile(Object(Rector\ValueObject\Application\File),
         Object(Rector\ValueObject\Configuration))
         #5 src/Application/ApplicationFileProcessor.php(134):
         Rector\Application\ApplicationFileProcessor->processFile(Object(Rector\ValueObject\Application\File),
         Object(Rector\ValueObject\Configuration))
         #6 src/Console/Command/WorkerCommand.php(139):
         Rector\Application\ApplicationFileProcessor->processFiles(Array, Object(Rector\ValueObject\Configuration),
         NULL)
         #7 vendor/evenement/evenement/src/EventEmitterTrait.php(143):
         Rector\Console\Command\WorkerCommand->Rector\Console\Command\{closure}(Array)
         #8 vendor/clue/ndjson-react/src/Decoder.php(139): Evenement\EventEmitter->emit('data', Array)
         #9 vendor/evenement/evenement/src/EventEmitterTrait.php(143): Clue\React\NDJson\Decoder->handleData(Array)
         #10 vendor/react/stream/src/Util.php(71): Evenement\EventEmitter->emit('data', Array)
         #11 vendor/evenement/evenement/src/EventEmitterTrait.php(143):
         React\Stream\Util::React\Stream\{closure}('{"action":"main...')
         #12 vendor/react/stream/src/DuplexResourceStream.php(209): Evenement\EventEmitter->emit('data', Array)
         #13 vendor/react/event-loop/src/StreamSelectLoop.php(246):
         React\Stream\DuplexResourceStream->handleData(Resource id #2206)
         #14 vendor/react/event-loop/src/StreamSelectLoop.php(213):
         React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)
         #15 src/Console/Command/WorkerCommand.php(89): React\EventLoop\StreamSelectLoop->run()
         #16 vendor/symfony/console/Command/Command.php(326):
         Rector\Console\Command\WorkerCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #17 vendor/symfony/console/Application.php(1078):
         Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #18 vendor/symfony/console/Application.php(324):
         Symfony\Component\Console\Application->doRunCommand(Object(Rector\Console\Command\WorkerCommand),
         Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
         #19 src/Console/ConsoleApplication.php(64):
         Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #20 vendor/symfony/console/Application.php(175):
         Rector\Console\ConsoleApplication->doRun(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #21 bin/rector.php(158): Symfony\Component\Console\Application->run()
         #22 bin/rector(4): require_once('/shared/backups...')
         #23 {main}". On line: 3



 [ERROR] Could not process "/shared/backups/bartlett/sarif-php-converters/examples/fixtures/php_bad_2.php" file, due to:
         "System error: "Syntax error, unexpected '}'"

         Stack trace:
         #0 src/PhpParser/Parser/RectorParser.php(40): PHPStan\Parser\RichParser->parseString('<?php\n\n/**\n * @...')
         #1 src/Application/FileProcessor.php(193):
         Rector\PhpParser\Parser\RectorParser->parseFileContentToStmtsAndTokens('<?php\n\n/**\n * @...')
         #2 src/Application/FileProcessor.php(114):
         Rector\Application\FileProcessor->parseFileNodes(Object(Rector\ValueObject\Application\File))
         #3 src/Application/FileProcessor.php(56):
         Rector\Application\FileProcessor->parseFileAndDecorateNodes(Object(Rector\ValueObject\Application\File))
         #4 src/Application/ApplicationFileProcessor.php(168):
         Rector\Application\FileProcessor->processFile(Object(Rector\ValueObject\Application\File),
         Object(Rector\ValueObject\Configuration))
         #5 src/Application/ApplicationFileProcessor.php(134):
         Rector\Application\ApplicationFileProcessor->processFile(Object(Rector\ValueObject\Application\File),
         Object(Rector\ValueObject\Configuration))
         #6 src/Console/Command/WorkerCommand.php(139):
         Rector\Application\ApplicationFileProcessor->processFiles(Array, Object(Rector\ValueObject\Configuration),
         NULL)
         #7 vendor/evenement/evenement/src/EventEmitterTrait.php(143):
         Rector\Console\Command\WorkerCommand->Rector\Console\Command\{closure}(Array)
         #8 vendor/clue/ndjson-react/src/Decoder.php(139): Evenement\EventEmitter->emit('data', Array)
         #9 vendor/evenement/evenement/src/EventEmitterTrait.php(143): Clue\React\NDJson\Decoder->handleData(Array)
         #10 vendor/react/stream/src/Util.php(71): Evenement\EventEmitter->emit('data', Array)
         #11 vendor/evenement/evenement/src/EventEmitterTrait.php(143):
         React\Stream\Util::React\Stream\{closure}('{"action":"main...')
         #12 vendor/react/stream/src/DuplexResourceStream.php(209): Evenement\EventEmitter->emit('data', Array)
         #13 vendor/react/event-loop/src/StreamSelectLoop.php(246):
         React\Stream\DuplexResourceStream->handleData(Resource id #2206)
         #14 vendor/react/event-loop/src/StreamSelectLoop.php(213):
         React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)
         #15 src/Console/Command/WorkerCommand.php(89): React\EventLoop\StreamSelectLoop->run()
         #16 vendor/symfony/console/Command/Command.php(326):
         Rector\Console\Command\WorkerCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #17 vendor/symfony/console/Application.php(1078):
         Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #18 vendor/symfony/console/Application.php(324):
         Symfony\Component\Console\Application->doRunCommand(Object(Rector\Console\Command\WorkerCommand),
         Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
         #19 src/Console/ConsoleApplication.php(64):
         Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #20 vendor/symfony/console/Application.php(175):
         Rector\Console\ConsoleApplication->doRun(Object(Symfony\Component\Console\Input\ArgvInput),
         Object(Symfony\Component\Console\Output\ConsoleOutput))
         #21 bin/rector.php(158): Symfony\Component\Console\Application->run()
         #22 bin/rector(4): require_once('/shared/backups...')
         #23 {main}". On line: 15

@llaville llaville added the bug label Jul 25, 2024
@llaville llaville changed the title Wrong Sytem-Error location Wrong System-Error location Jul 25, 2024
@llaville
Copy link
Author

PR rectorphp/rector-src#6184 is running

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

Successfully merging a pull request may close this issue.

1 participant