8000 [DomCrawler] [5.0] add type-hint whenever possible · symfony/symfony@580b126 · GitHub
[go: up one dir, main page]

Skip to content

Commit 580b126

Browse files
committed
[DomCrawler] [5.0] add type-hint whenever possible
1 parent 8fb4741 commit 580b126

File tree

9 files changed

+52
-135
lines changed

9 files changed

+52
-135
lines changed

src/Symfony/Component/DomCrawler/AbstractUriElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ abstract protected function getRawUri();
136136
*
137137
* @return string
138138
*/
139-
protected function canonicalizePath($path)
139+
protected function canonicalizePath(string $path)
140140
{
141141
if ('' === $path || '/' === $path) {
142142
return $path;

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ class Crawler implements \Countable, \IteratorAggregate
6161
private $html5Parser;
6262

6363
/**
64-
* @param mixed $node A Node to use as the base for the crawling
65-
* @param string $uri The current URI
66-
* @param string $baseHref The base href value
64+
* @param mixed $node A Node to use as the base for the crawling
6765
*/
6866
public function __construct($node = null, string $uri = null, string $baseHref = null)
6967
{
@@ -134,11 +132,8 @@ public function add($node)
134132
* If the charset is not set via the content type, it is assumed to be UTF-8,
135133
* or ISO-8859-1 as a fallback, which is the default charset defined by the
136134
* HTTP 1.1 specification.
137-
*
138-
* @param string $content A string to parse as HTML/XML
139-
* @param string|null $type The content type of the string
140135
*/
141-
public function addContent($content, $type = null)
136+
public function addContent(string $content, string $type = null)
142137
{
143138
if (empty($type)) {
144139
$type = 0 === strpos($content, '<?xml') ? 'application/xml' : 'text/html';
@@ -184,11 +179,8 @@ public function addContent($content, $type = null)
184179
* internal errors via libxml_use_internal_errors(true)
185180
* and then, get the errors via libxml_get_errors(). Be
186181
* sure to clear errors with libxml_clear_errors() afterward.
187-
*
188-
* @param string $content The HTML content
189-
* @param string $charset The charset
190182
*/
191-
public function addHtmlContent($content, $charset = 'UTF-8')
183+
public function addHtmlContent(string $content, string $charset = 'UTF-8')
192184
{
193185
// Use HTML5 parser if the content is HTML5 and the library is available
194186
$dom = null !== $this->html5Parser && strspn($content, " \t\r\n") === stripos($content, '<!doctype html>') ? $this->parseHtml5($content, $charset) : $this->parseXhtml($content, $charset);
@@ -219,13 +211,11 @@ public function addHtmlContent($content, $charset = 'UTF-8')
219211
* and then, get the errors via libxml_get_errors(). Be
220212
* sure to clear errors with libxml_clear_errors() afterward.
221213
*
222-
* @param string $content The XML content
223-
* @param string $charset The charset
224-
* @param int $options Bitwise OR of the libxml option constants
225-
* LIBXML_PARSEHUGE is dangerous, see
226-
* http://symfony.com/blog/security-release-symfony-2-0-17-released
214+
* @param int $options Bitwise OR of the libxml option constants
215+
* LIBXML_PARSEHUGE is dangerous, see
216+
* http://symfony.com/blog/security-release-symfony-2-0-17-released
227217
*/
228-
public function addXmlContent($content, $charset = 'UTF-8', $options = LIBXML_NONET)
218+
public function addXmlContent(string $content, string $charset = 'UTF-8', int $options = LIBXML_NONET)
229219
{
230220
// remove the default namespace if it's the only namespace to make XPath expressions simpler
231221
if (!preg_match('/xmlns:/', $content)) {
@@ -318,11 +308,9 @@ public function addNode(\DOMNode $node)
318308
/**
319309
* Returns a node given its position in the node list.
320310
*
321-
* @param int $position The position
322-
*
323311
* @return self
324312
*/
325-
public function eq($position)
313+
public function eq(int $position)
326314
{
327315
if (isset($this->nodes[$position])) {
328316
return $this->createSubCrawler($this->nodes[$position]);
@@ -360,12 +348,9 @@ public function each(\Closure $closure)
360348
/**
361349
* Slices the list of nodes by $offset and $length.
362350
*
363-
* @param int $offset
364-
* @param int $length
365-
*
366351
* @return self
367352
*/
368-
public function slice($offset = 0, $length = null)
353+
public function slice(int $offset = 0, int $length = null)
369354
{
370355
return $this->createSubCrawler(\array_slice($this->nodes, $offset, $length));
371356
}
@@ -487,8 +472,6 @@ public function parents()
487472
/**
488473
* Returns the children nodes of the current selection.
489474
*
490-
* @param string|null $selector An optional CSS selector to filter children
491-
*
492475
* @return self
493476
*
494477
* @throws \InvalidArgumentException When current node is empty
@@ -515,13 +498,11 @@ public function children(string $selector = null)
515498
/**
516499
* Returns the attribute value of the first node of the list.
517500
*
518-
* @param string $attribute The attribute name
519-
*
520501
* @return string|null The attribute value or null if the attribute does not exist
521502
*
522503
* @throws \InvalidArgumentException When current node is empty
523504
*/
524-
public function attr($attribute)
505+
public function attr(string $attribute)
525506
{
526507
if (!$this->nodes) {
527508
throw new \InvalidArgumentException('The current node list is empty.');
@@ -610,11 +591,9 @@ public function html($default = null)
610591
* Since an XPath expression might evaluate to either a simple type or a \DOMNodeList,
611592
* this method will return either an array of simple types or a new Crawler instance.
612593
*
613-
* @param string $xpath An XPath expression
614-
*
615594
* @return array|Crawler An array of evaluation results or a new Crawler instance
616595
*/
617-
public function evaluate($xpath)
596+
public function evaluate(string $xpath)
618597
{
619598
if (null === $this->document) {
620599
throw new \LogicException('Cannot evaluate the expression on an uninitialized crawler.');
@@ -643,13 +622,10 @@ public function evaluate($xpath)
643622
*
644623
* $crawler->filter('h1 a')->extract(['_text', 'href']);
645624
*
646-
* @param array $attributes An array of attributes
647-
*
648625
* @return array An array of extracted values
649626
*/
650-
public function extract($attributes)
627+
public function extract(array $attributes)
651628
{
652-
$attributes = (array) $attributes;
653629
$count = \count($attributes);
654630

655631
$data = [];
@@ -679,11 +655,9 @@ public function extract($attributes)
679655
* This means that a child selector "div" or "./div" will match only
680656
* the div elements of the current crawler, not their children.
681657
*
682-
* @param string $xpath An XPath expression
683-
*
684658
* @return self
685659
*/
686-
public function filterXPath($xpath)
660+
public function filterXPath(string $xpath)
687661
{
688662
$xpath = $this->relativize($xpath);
689663

@@ -700,13 +674,11 @@ public function filterXPath($xpath)
700674
*
701675
* This method only works if you have installed the CssSelector Symfony Component.
702676
*
703-
* @param string $selector A CSS selector
704-
*
705677
* @return self
706678
*
707679
* @throws \RuntimeException if the CssSelector Component is not available
708680
*/
709-
public function filter($selector)
681+
public function filter(string $selector)
710682
{
711683
$converter = $this->createCssSelectorConverter();
712684

@@ -717,11 +689,9 @@ public function filter($selector)
717689
/**
718690
* Selects links by name or alt value for clickable images.
719691
*
720-
* @param string $value The link text
721-
*
722692
* @return self
723693
*/
724-
public function selectLink($value)
694+
public function selectLink(string $value)
725695
{
726696
return $this->filterRelativeXPath(
727697
sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %1$s) or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %1$s)]]', static::xpathLiteral(' '.$value.' '))
@@ -731,11 +701,9 @@ public function selectLink($value)
731701
/**
732702
* Selects images by alt value.
733703
*
734-
* @param string $value The image alt
735-
*
736704
* @return self A new instance of Crawler with the filtered list of nodes
737705
*/
738-
public function selectImage($value)
706+
public function selectImage(string $value)
739707
{
740708
$xpath = sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', static::xpathLiteral($value));
741709

@@ -745,11 +713,9 @@ public function selectImage($value)
745713
/**
746714
* Selects a button by name or alt value for images.
747715
*
748-
* @param string $value The button text
749-
*
750716
* @return self
751717
*/
752-
public function selectButton($value)
718+
public function selectButton(string $value)
753719
{
754720
return $this->filterRelativeXPath(
755721
sprintf('descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or @id=%3$s or @name=%3$s]', 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value))
@@ -759,13 +725,11 @@ public function selectButton($value)
759725
/**
760726
* Returns a Link object for the first node in the list.
761727
*
762-
* @param string $method The method for the link (get by default)
763-
*
764728
* @return Link A Link instance
765729
*
766730
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
767731
*/
768-
public function link($method = 'get')
732+
public function link(string $method = 'get')
769733
{
770734
if (!$this->nodes) {
771735
throw new \InvalidArgumentException('The current node list is empty.');
@@ -845,14 +809,11 @@ public function images()
845809
/**
846810
* Returns a Form object for the first node in the list.
847811
*
848-
* @param array $values An array of values for the form fields
849-
* @param string $method The method for the form
850-
*
851812
* @return Form A Form instance
852813
*
853814
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
854815
*/
855-
public function form(array $values = null, $method = null)
816+
public function form(array $values = null, string $method = null)
856817
{
857818
if (!$this->nodes) {
858819
throw new \InvalidArgumentException('The current node list is empty.');
@@ -875,19 +836,13 @@ public function form(array $values = null, $method = null)
875836

876837
/**
877838
* Overloads a default namespace prefix to be used with XPath and CSS expressions.
878-
*
879-
* @param string $prefix
880839
*/
881-
public function setDefaultNamespacePrefix($prefix)
840+
public function setDefaultNamespacePrefix(string $prefix)
882841
{
883842
$this->defaultNamespacePrefix = $prefix;
884843
}
885844

886-
/**
887-
* @param string $prefix
888-
* @param string $namespace
889-
*/
890-
public function registerNamespace($prefix, $namespace)
845+
public function registerNamespace(string $prefix, string $namespace)
891846
{
892847
$this->namespaces[$prefix] = $namespace;
893848
}
@@ -909,11 +864,9 @@ public function registerNamespace($prefix, $namespace)
909864
* //prints concat('a', "'", 'b"c')
910865
*
911866
*
912-
* @param string $s String to be escaped
913-
*
914867
* @return string Converted string
915868
*/
916-
public static function xpathLiteral($s)
869+
public static function xpathLiteral(string $s)
917870
{
918871
if (false === strpos($s, "'")) {
919872
return sprintf("'%s'", $s);
@@ -944,11 +897,9 @@ public static function xpathLiteral($s)
944897
*
945898
* The XPath expression should already be processed to apply it in the context of each node.
946899
*
947-
* @param string $xpath
948-
*
949900
* @return self
950901
*/
951-
private function filterRelativeXPath($xpath)
902+
private function filterRelativeXPath(string $xpath)
952903
{
953904
$prefixes = $this->findNamespacePrefixes($xpath);
954905

@@ -1053,11 +1004,9 @@ private function relativize(string $xpath): string
10531004
}
10541005

10551006
/**
1056-
* @param int $position
1057-
*
10581007
* @return \DOMElement|null
10591008
*/
1060-
public function getNode($position)
1009+
public function getNode(int $position)
10611010
{
10621011
if (isset($this->nodes[$position])) {
10631012
return $this->nodes[$position];
@@ -1082,11 +1031,10 @@ public function getIterator()
10821031

10831032
/**
10841033
* @param \DOMElement $node
1085-
* @param string $siblingDir
10861034
*
10871035
* @return array
10881036
*/
1089-
protected function sibling($node, $siblingDir = 'nextSibling')
1037+
protected function sibling($node, string $siblingDir = 'nextSibling')
10901038
{
10911039
$nodes = [];
10921040

src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,9 @@ private function buildOptionValue(\DOMElement $node): array
270270
/**
271271
* Checks whether given value is in the existing options.
272272
*
273-
* @param string $optionValue
274-
* @param array $options
275-
*
276273
* @return bool
277274
*/
278-
public function containsOption($optionValue, $options)
275+
public function containsOption(string $optionValue, array $options)
279276
{
280277
if ($this->validationDisabled) {
281278
return true;

src/Symfony/Component/DomCrawler/Field/FileFormField.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class FileFormF 10000 ield extends FormField
2525
*
2626
* @throws \InvalidArgumentException When error code doesn't exist
2727
*/
28-
public function setErrorCode($error)
28+
public function setErrorCode(int $error)
2929
{
3030
$codes = [UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION];
3131
if (!\in_array($error, $codes)) {
@@ -37,20 +37,16 @@ public function setErrorCode($error)
3737

3838
/**
3939
* Sets the value of the field.
40-
*
41-
* @param string $value The value of the field
4240
*/
43-
public function upload($value)
41+
public function upload(?string $value)
4442
{
4543
$this->setValue($value);
4644
}
4745

4846
/**
4947
* Sets the value of the field.
50-
*
51-
* @param string $value The value of the field
5248
*/
53-
public function setValue($value)
49+
public function setValue(?string $value)
5450
{
5551
if (null !== $value && is_readable($value)) {
5652
$error = UPLOAD_ERR_OK;
@@ -80,10 +76,8 @@ public function setValue($value)
8076

8177
/**
8278
* Sets path to the file as string for simulating HTTP request.
83-
*
84-
* @param string $path The path to the file
8579
*/
86-
public function setFilePath($path)
80+
public function setFilePath(string $path)
8781
{
8882
parent::setValue($path);
8983
}

src/Symfony/Component/DomCrawler/Field/FormField.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ public function getValue()
9999

100100
/**
101101
* Sets the value of the field.
102-
*
103-
* @param string $value The value of the field
104102
*/
105-
public function setValue($value)
103+
public function setValue(?string $value)
106104
{
107105
$this->value = (string) $value;
108106
}

0 commit comments

Comments
 (0)
0