Skip to content

Commit

Permalink
feature #23332 [Yaml] fix the displayed line number (fabpot, xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml] fix the displayed line number

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23294
| License       | MIT
| Doc PR        |

First, this PR backports #23294 to the `3.4` branch.

Secondly, `getRealCurrentLineNb()` returns line numbers index by 0 (as they serve as array indexes internally). I removed the `getLastLineNumberBeforeDeprecation()` method added in #23294 as we can just expose the already existing `getRealCurrentLineNb()` method.

Commits
-------

a2ae6bf [Yaml] fix the displayed line number
1baac5a feature #23294 [Yaml][Lint] Add line numbers to JSON output. (WybrenKoelmans)
  • Loading branch information
fabpot committed Jun 30, 2017
2 parents e891d55 + a2ae6bf commit 7787343
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Command/LintCommand.php
Expand Up @@ -145,7 +145,7 @@ private function validate(Environment $twig, $template, $file)
} catch (Error $e) {
$twig->setLoader($realLoader);

return array('template' => $template, 'file' => $file, 'valid' => false, 'exception' => $e);
return array('template' => $template, 'file' => $file, 'line' => $e->getTemplateLine(), 'valid' => false, 'exception' => $e);
}

return array('template' => $template, 'file' => $file, 'valid' => true);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Yaml/Command/LintCommand.php
Expand Up @@ -105,7 +105,7 @@ private function validate($content, $file = null)
{
$prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) {
if (E_USER_DEPRECATED === $level) {
throw new ParseException($message);
throw new ParseException($message, $this->getParser()->getRealCurrentLineNb() + 1);
}

return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
Expand All @@ -114,7 +114,7 @@ private function validate($content, $file = null)
try {
$this->getParser()->parse($content, Yaml::PARSE_CONSTANT);
} catch (ParseException $e) {
return array('file' => $file, 'valid' => false, 'message' => $e->getMessage());
return array('file' => $file, 'line' => $e->getParsedLine(), 'valid' => false, 'message' => $e->getMessage());
} finally {
restore_error_handler();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Yaml/Parser.php
Expand Up @@ -453,9 +453,11 @@ private function parseBlock($offset, $yaml, $flags)
/**
* Returns the current line number (takes the offset into account).
*
* @internal
*
* @return int The current line number
*/
private function getRealCurrentLineNb()
public function getRealCurrentLineNb()
{
$realCurrentLineNumber = $this->currentLineNb + $this->offset;

Expand Down

0 comments on commit 7787343

Please sign in to comment.