Skip to content

Commit 14e9070

Browse files
kevinjhappynicolas-grekas
authored andcommitted
Trigger deprecation notices when inherited class calls parent method but misses adding new arguments
1 parent d980fee commit 14e9070

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* The `$currentUri` constructor argument of the `AbstractUriElement`, `Link` and
88
`Image` classes is now optional.
9+
* The `Crawler::children()` method will have a new `$selector` argument in version 5.0,
10+
not defining it is deprecated since version 4.2.
911

1012
3.1.0
1113
-----

Crawler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ public function parents()
510510
*/
511511
public function children(/* string $selector = null */)
512512
{
513+
if (\func_num_args() < 1 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface) {
514+
@trigger_error(sprintf('The "%s()" method will have a new "string $selector = null" argument in version 5.0, not defining it is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
515+
}
513516
$selector = 0 < \func_num_args() ? func_get_arg(0) : null;
514517

515518
if (!$this->nodes) {

Tests/CrawlerTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,62 @@ public function testEvaluateThrowsAnExceptionIfDocumentIsEmpty()
11481148
(new Crawler())->evaluate('//form/input[1]');
11491149
}
11501150

1151+
/**
1152+
* @group legacy
1153+
* @expectedDeprecation The "Symfony\Component\DomCrawler\Crawler::children()" method will have a new "string $selector = null" argument in version 5.0, not defining it is deprecated since Symfony 4.2.
1154+
*/
1155+
public function testInheritedClassCallChildrenWithoutArgument()
1156+
{
1157+
$dom = new \DOMDocument();
1158+
$dom->loadHTML('
1159+
<html>
1160+
<body>
1161+
<a href="foo">Foo</a>
1162+
<a href="/foo"> Fabien\'s Foo </a>
1163+
<a href="/foo">Fabien"s Foo</a>
1164+
<a href="/foo">\' Fabien"s Foo</a>
1165+
1166+
<a href="/bar"><img alt="Bar"/></a>
1167+
<a href="/bar"><img alt=" Fabien\'s Bar "/></a>
1168+
<a href="/bar"><img alt="Fabien&quot;s Bar"/></a>
1169+
<a href="/bar"><img alt="\' Fabien&quot;s Bar"/></a>
1170+
1171+
<a href="?get=param">GetLink</a>
1172+
1173+
<a href="/example">Klausi|Claudiu</a>
1174+
1175+
<form action="foo" id="FooFormId">
1176+
<input type="text" value="TextValue" name="TextName" />
1177+
<input type="submit" value="FooValue" name="FooName" id="FooId" />
1178+
<input type="button" value="BarValue" name="BarName" id="BarId" />
1179+
<button value="ButtonValue" name="ButtonName" id="ButtonId" />
1180+
</form>
1181+
1182+
<input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
1183+
<input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
1184+
1185+
<ul class="first">
1186+
<li class="first">One</li>
1187+
<li>Two</li>
1188+
<li>Three</li>
1189+
</ul>
1190+
<ul>
1191+
<li>One Bis</li>
1192+
<li>Two Bis</li>
1193+
<li>Three Bis</li>
1194+
</ul>
1195+
<div id="parent">
1196+
<div id="child"></div>
1197+
<div id="child2" xmlns:foo="http://example.com"></div>
1198+
</div>
1199+
<div id="sibling"><img /></div>
1200+
</body>
1201+
</html>
1202+
');
1203+
$crawlerChild = new ClassThatInheritCrawler($dom);
1204+
$crawlerChild->children();
1205+
}
1206+
11511207
public function createTestCrawler($uri = null)
11521208
{
11531209
$dom = new \DOMDocument();
@@ -1234,3 +1290,11 @@ protected function createNodeList()
12341290
return $domxpath->query('//div');
12351291
}
12361292
}
1293+
1294+
class ClassThatInheritCrawler extends Crawler
1295+
{
1296+
public function children()
1297+
{
1298+
parent::children();
1299+
}
1300+
}

0 commit comments

Comments
 (0)