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

Skip to content

Commit 196727a

Browse files
committed
include file and line number in deprecation
1 parent 3f4c47c commit 196727a

File tree

6 files changed

+38
-10
lines changed

6 files changed

+38
-10
lines changed

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

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

608+
$prevErrorHandler = set_error_handler(function ($level, $message, $script, $line, $context) use ($file, &$prevErrorHandler) {
609+
if (E_USER_DEPRECATED === $level && false !== $pos = strpos($message, ' on line ')) {
610+
$message = sprintf('%s in "%s"%s', substr($message, 0, $pos), $file, substr($message, $pos));
611+
}
612+
613+
return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line, $context) : false;
614+
});
615+
608616
try {
609617
$configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_KEYS_AS_STRINGS);
610618
} catch (ParseException $e) {
611619
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
620+
} finally {
621+
restore_error_handler();
612622
}
613623

614624
return $this->validate($configuration, $file);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
foo:
3+
class: Foo
4+
bar:
5+
class: Bar
6+
foo:
7+
class: Baz

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ public function provideInvalidFiles()
9494
);
9595
}
9696

97+
/**
98+
* @group legacy
99+
* @expectedDeprecation Duplicate key "foo" detected in "%sduplicated_keys.yml" on line 6 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.
100+
*/
101+
public function testLoadFileContainingDeprecations()
102+
{
103+
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator(self::$fixturesPath.'/yaml'));
104+
105+
$loader->load('duplicated_keys.yml');
106+
}
107+
97108
public function testLoadParameters()
98109
{
99110
$container = new ContainerBuilder();

src/Symfony/Component/Yaml/Inline.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 on line %d 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, 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 on line %d 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, 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 on line %d 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, self::$parsedLineNumber + 1), E_USER_DEPRECATED);
544544
$duplicate = true;
545545
}
546546
--$i;

src/Symfony/Component/Yaml/Parser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 on line %d is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.', $keyType, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
242242
}
243243

244244
// Convert float keys to strings, to avoid being converted to integers by PHP
@@ -312,7 +312,7 @@ private function doParse($value, $flags)
312312
$data[$key] = null;
313313
}
314314
} else {
315-
@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);
315+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d 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, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
316316
}
317317
} else {
318318
// remember the parsed line number here in case we need it to provide some contexts in error messages below
@@ -327,7 +327,7 @@ private function doParse($value, $flags)
327327
$data[$key] = $value;
328328
}
329329
} else {
330-
@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);
330+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d 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, $realCurrentLineNbKey + 1), E_USER_DEPRECATED);
331331
}
332332
}
333333
} else {
@@ -337,7 +337,7 @@ private function doParse($value, $flags)
337337
if ($allowOverwrite || !isset($data[$key])) {
338338
$data[$key] = $value;
339339
} else {
340-
@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);
340+
@trigger_error(sprintf('Duplicate key "%s" detected on line %d 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, $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED);
341341
}
342342
}
343343
if ($isRef) {

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ public function testMappingDuplicateKeyFlow()
853853
/**
854854
* @group legacy
855855
* @dataProvider getParseExceptionOnDuplicateData
856-
* @expectedDeprecation Duplicate key "%s" detected whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated %s.
856+
* @expectedDeprecation Duplicate key "%s" detected on line %d whilst parsing YAML. Silent handling of duplicate mapping keys in YAML is deprecated %s.
857857
* throws \Symfony\Component\Yaml\Exception\ParseException in 4.0
858858
*/
859859
public function testParseExceptionOnDuplicate($input, $duplicateKey, $lineNumber)
@@ -1081,7 +1081,7 @@ public function testYamlDirective()
10811081

10821082
/**
10831083
* @group legacy
1084-
* @expectedDeprecation Implicit casting of numeric key 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.
1084+
* @expectedDeprecation Implicit casting of numeric key to string on line 2 is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.
10851085
*/
10861086
public function testFloatKeys()
10871087
{
@@ -1103,7 +1103,7 @@ public function testFloatKeys()
11031103

11041104
/**
11051105
* @group legacy
1106-
* @expectedDeprecation Implicit casting of non-string key 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.
1106+
* @expectedDeprecation Implicit casting of non-string key to string on line 1 is deprecated since version 3.3 and will throw \Symfony\Component\Yaml\Exception\ParseException in 4.0. Quote your evaluable mapping keys instead.
11071107
*/
11081108
public function testBooleanKeys()
11091109
{

0 commit comments

Comments
 (0)