Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0063800

Browse files
bug #24191 [DependencyInjection] include file and line number in deprecation (xabbuh)
This PR was merged into the 3.3 branch. Discussion ---------- [DependencyInjection] include file and line number in deprecation | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | In #23108 we removed line numbers from deprecation messages created by the YAML parser because those numbers were quite useless without the file being parsed. I suggest to revert this change and add the file being parsed to the deprecation message. Commits ------- cf03552 include file and line number in deprecation
2 parents 29433fa + cf03552 commit 0063800

File tree

9 files changed

+72
-30
lines changed

9 files changed

+72
-30
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,18 @@ protected function loadFile($file)
605605
$this->yamlParser = new YamlParser();
606606
}
607607

608+
$prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) {
609+
$message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message;
610+
611+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
612+
});
613+
608614
try {
609615
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_KEYS_AS_STRINGS);
610616
} catch (ParseException $e) {
611617
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
618+
} finally {
619+
restore_error_handler();
612620
}
613621

614622
return $this->validate($configuration, $file);

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,18 @@ public function load($file, $type = null)
5858
$this->yamlParser = new YamlParser();
5959
}
6060

61+
$prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) {
62+
$message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message;
63+
64+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
65+
});
66+
6167
try {
6268
$parsedConfig = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS);
6369
} catch (ParseException $e) {
6470
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
71+
} finally {
72+
restore_error_handler();
6573
}
6674

6775
$collection = new RouteCollection();

src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,17 @@ private function getClassesFromYaml()
114114
$this->yamlParser = new Parser();
115115
}
116116

117-
$classes = $this->yamlParser->parse(file_get_contents($this->file), Yaml::PARSE_KEYS_AS_STRINGS);
117+
$prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use (&$prevErrorHandler) {
118+
$message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$this->file.'"$0', $message) : $message;
119+
120+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
121+
});
122+
123+
try {
124+
$classes = $this->yamlParser->parse(file_get_contents($this->file), Yaml::PARSE_KEYS_AS_STRINGS);
125+
} finally {
126+
restore_error_handler();
127+
}
118128

119129
if (empty($classes)) {
120130
return array();

src/Symfony/Component/Translation/Loader/YamlFileLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ protected function loadResource($resource)
3939
$this->yamlParser = new YamlParser();
4040
}
4141

42+
$prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($resource, &$prevErrorHandler) {
43+
$message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$resource.'"$0', $message) : $message;
44+
45+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
46+
});
47+
4248
try {
4349
$messages = $this->yamlParser->parse(file_get_contents($resource), Yaml::PARSE_KEYS_AS_STRINGS);
4450
} catch (ParseException $e) {
4551
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $resource), 0, $e);
52+
} finally {
53+
restore_error_handler();
4654
}
4755

4856
return $messages;

src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,18 @@ protected function parseNodes(array $nodes)
115115
*/
116116
private function parseFile($path)
117117
{
118+
$prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($path, &$prevErrorHandler) {
119+
$message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$path.'"$0', $message) : $message;
120+
121+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
122+
});
123+
118124
try {
119125
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS);
120126
} catch (ParseException $e) {
121127
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
128+
} finally {
129+
restore_error_handler();
122130
}
123131

124132
// empty file

src/Symfony/Component/Yaml/Inline.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public static function parseScalar($scalar, $flags = 0, $delimiters = null, &$i
335335
}
336336

337337
if ($output && '%' === $output[0]) {
338-
@trigger_error(sprintf('Not quoting the scalar "%s" starting with the "%%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', $output), E_USER_DEPRECATED);
338+
@trigger_error(sprintf('Not quoting the scalar "%s" starting with the "%%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0 on line %d.', $output, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
339339
}
340340

341341
if ($evaluate) {
@@ -487,19 +487,19 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
487487
}
488488

489489
if (':' === $key) {
490-
@trigger_error('Omitting the key of a mapping is deprecated and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
490+
@trigger_error(sprintf('Omitting the key of a mapping is deprecated and will throw a ParseException in 4.0 on line %d.', self::$parsedLineNumber + 1), E_USER_DEPRECATED);
491491
}
492492

493493
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags)) {
494494
$evaluatedKey = self::evaluateScalar($key, $flags, $references);
495495

496496
if ('' !== $key && $evaluatedKey !== $key && !is_string($evaluatedKey) && !is_int($evaluatedKey)) {
497-
@trigger_error('Implicit casting of incompatible mapping keys to strings is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.', E_USER_DEPRECATED);
497+
@trigger_error(sprintf('Implicit casting of incompatible mapping keys to strings is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead on line %d.', self::$parsedLineNumber + 1), E_USER_DEPRECATED);
498498
}
499499
}
500500

501501
if (':' !== $key && !$isKeyQuoted && (!isset($mapping[$i + 1]) || !in_array($mapping[$i + 1], array(' ', ',', '[', ']', '{', '}'), true))) {
502-
@trigger_error('Using a colon after an unquoted mapping key that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}") is deprecated since version 3.2 and will throw a ParseException in 4.0.', E_USER_DEPRECATED);
502+
@trigger_error(sprintf('Using a colon after an unquoted mapping key that is not followed by an indication character (i.e. " ", ",", "[", "]", "{", "}") is deprecated since version 3.2 and will throw a ParseException in 4.0 on line %d.', self::$parsedLineNumber + 1), E_USER_DEPRECATED);
503503
}
504504

505505
while ($i < $len) {
@@ -519,7 +519,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
519519
// Parser cannot abort this mapping earlier, since lines
520520
// are processed sequentially.
521521
if (isset($output[$key])) {
522-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
522+
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
523523
$duplicate = true;
524524
}
525525
break;
@@ -530,7 +530,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
530530
// Parser cannot abort this mapping earlier, since lines
531531
// are processed sequentially.
532532
if (isset($output[$key])) {
533-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
533+
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
534534
$duplicate = true;
535535
}
536536
break;
@@ -540,7 +540,7 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = ar
540540
// Parser cannot abort this mapping earlier, since lines
541541
// are processed sequentially.
542542
if (isset($output[$key])) {
543-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
543+
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $key, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
544544
$duplicate = true;
545545
}
546546
--$i;
@@ -624,7 +624,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
624624
return;
625625
case 0 === strpos($scalar, '!!php/object:'):
626626
if (self::$objectSupport) {
627-
@trigger_error('The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead.', E_USER_DEPRECATED);
627+
@trigger_error(sprintf('The !!php/object tag to indicate dumped PHP objects is deprecated since version 3.1 and will be removed in 4.0. Use the !php/object tag instead on line %d.', self::$parsedLineNumber + 1), E_USER_DEPRECATED);
628628

629629
return unserialize(substr($scalar, 13));
630630
}
@@ -652,7 +652,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
652652
case 0 === strpos($scalar, '!!binary '):
653653
return self::evaluateBinaryScalar(substr($scalar, 9));
654654
default:
655-
@trigger_error(sprintf('Using the unquoted scalar value "%s" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it.', $scalar), E_USER_DEPRECATED);
655+
@trigger_error(sprintf('Using the unquoted scalar value "%s" is deprecated since version 3.3 and will be considered as a tagged value in 4.0. You must quote it on line %d.', $scalar, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
656656
}
657657

658658
// Optimize for returning strings.
@@ -686,7 +686,7 @@ private static function evaluateScalar($scalar, $flags, $references = array())
686686
case Parser::preg_match('/^(-|\+)?[0-9][0-9,]*(\.[0-9_]+)?$/', $scalar):
687687
case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
688688
if (false !== strpos($scalar, ',')) {
689-
@trigger_error('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0.', E_USER_DEPRECATED);
689+
@trigger_error(sprintf('Using the comma as a group separator for floats is deprecated since version 3.2 and will be removed in 4.0 on line %d.', self::$parsedLineNumber + 1), E_USER_DEPRECATED);
690690
}
691691

692692
return (float) str_replace(array(',', '_'), '', $scalar);

src/Symfony/Component/Yaml/Parser.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private function doParse($value, $flags)
178178
}
179179

180180
if (isset($values['value'][1]) && '?' === $values['value'][0] && ' ' === $values['value'][1]) {
181-
@trigger_error('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', E_USER_DEPRECATED);
181+
@trigger_error(sprintf('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
182182
}
183183

184184
// array
@@ -238,7 +238,7 @@ private function doParse($value, $flags)
238238

239239
if (!(Yaml::PARSE_KEYS_AS_STRINGS & $flags) && !is_string($key) && !is_int($key)) {
240240
$keyType = is_numeric($key) ? 'numeric key' : 'non-string key';
241-
@trigger_error(sprintf('Implicit casting of %s to string is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.', $keyType), E_USER_DEPRECATED);
241+
@trigger_error(sprintf('Implicit casting of %s to string is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead on line %d.', $keyType, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
242242
}
243243

244244
// Convert float keys to strings, to avoid being converted to integers by PHP
@@ -324,7 +324,7 @@ private function doParse($value, $flags)
324324
$data[$key] = null;
325325
}
326326
} else {
327-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
327+
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $key, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
328328
}
329329
} else {
330330
// remember the parsed line number here in case we need it to provide some contexts in error messages below
@@ -339,7 +339,7 @@ private function doParse($value, $flags)
339339
$data[$key] = $value;
340340
}
341341
} else {
342-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
342+
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $key, $realCurrentLineNbKey + 1), E_USER_DEPRECATED);
343343
}
344344
}
345345
} else {
@@ -349,7 +349,7 @@ private function doParse($value, $flags)
349349
if ($allowOverwrite || !isset($data[$key])) {
350350
$data[$key] = $value;
351351
} else {
352-
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', $key), E_USER_DEPRECATED);
352+
@trigger_error(sprintf('Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated since version 3.2 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $key, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
353353
}
354354
}
355355
if ($isRef) {
@@ -362,7 +362,7 @@ private function doParse($value, $flags)
362362
}
363363

364364
if (isset($this->currentLine[1]) && '?' === $this->currentLine[0] && ' ' === $this->currentLine[1]) {
365-
@trigger_error('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0.', E_USER_DEPRECATED);
365+
@trigger_error(sprintf('Starting an unquoted string with a question mark followed by a space is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0 on line %d.', $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
366366
}
367367

368368
// 1-liner optionally followed by newline(s)
@@ -675,7 +675,7 @@ private function parseValue($value, $flags, $context)
675675
if ('!!binary' === $matches['tag']) {
676676
return Inline::evaluateBinaryScalar($data);
677677
} elseif ('!' !== $matches['tag']) {
678-
@trigger_error(sprintf('Using the custom tag "%s" for the value "%s" is deprecated since version 3.3. It will be replaced by an instance of %s in 4.0.', $matches['tag'], $data, TaggedValue::class), E_USER_DEPRECATED);
678+
@trigger_error(sprintf('Using the custom tag "%s" for the value "%s" is deprecated since version 3.3. It will be replaced by an instance of %s in 4.0 on line %d.', $matches['tag'], $data, TaggedValue::class, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
679679
}
680680
}
681681

0 commit comments

Comments
 (0)