Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DomCrawler] [5.0] add type-hint whenever possible #32329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/DomCrawler/AbstractUriElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ abstract protected function getRawUri();
*
* @return string
*/
protected function canonicalizePath($path)
protected function canonicalizePath(string $path)
{
if ('' === $path || '/' === $path) {
return $path;
Expand Down
102 changes: 25 additions & 77 deletions 102 src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ class Crawler implements \Countable, \IteratorAggregate
private $html5Parser;

/**
* @param mixed $node A Node to use as the base for the crawling
* @param string $uri The current URI
* @param string $baseHref The base href value
Simperfit marked this conversation as resolved.
Show resolved Hide resolved
* @param mixed $node A Node to use as the base for the crawling
*/
public function __construct($node = null, string $uri = null, string $baseHref = null)
{
Expand Down Expand Up @@ -134,11 +132,8 @@ public function add($node)
* If the charset is not set via the content type, it is assumed to be UTF-8,
* or ISO-8859-1 as a fallback, which is the default charset defined by the
* HTTP 1.1 specification.
*
* @param string $content A string to parse as HTML/XML
* @param string|null $type The content type of the string
*/
public function addContent($content, $type = null)
public function addContent(string $content, string $type = null)
javiereguiluz marked this conversation as resolved.
Show resolved Hide resolved
{
if (empty($type)) {
$type = 0 === strpos($content, '<?xml') ? 'application/xml' : 'text/html';
Expand Down Expand Up @@ -184,11 +179,8 @@ public function addContent($content, $type = null)
* internal errors via libxml_use_internal_errors(true)
* and then, get the errors via libxml_get_errors(). Be
* sure to clear errors with libxml_clear_errors() afterward.
*
* @param string $content The HTML content
* @param string $charset The charset
*/
public function addHtmlContent($content, $charset = 'UTF-8')
public function addHtmlContent(string $content, string $charset = 'UTF-8')
{
// Use HTML5 parser if the content is HTML5 and the library is available
$dom = null !== $this->html5Parser && strspn($content, " \t\r\n") === stripos($content, '<!doctype html>') ? $this->parseHtml5($content, $charset) : $this->parseXhtml($content, $charset);
Expand Down Expand Up @@ -219,13 +211,11 @@ public function addHtmlContent($content, $charset = 'UTF-8')
* and then, get the errors via libxml_get_errors(). Be
* sure to clear errors with libxml_clear_errors() afterward.
*
* @param string $content The XML content
* @param string $charset The charset
* @param int $options Bitwise OR of the libxml option constants
* LIBXML_PARSEHUGE is dangerous, see
* http://symfony.com/blog/security-release-symfony-2-0-17-released
* @param int $options Bitwise OR of the libxml option constants
* LIBXML_PARSEHUGE is dangerous, see
* http://symfony.com/blog/security-release-symfony-2-0-17-released
*/
public function addXmlContent($content, $charset = 'UTF-8', $options = LIBXML_NONET)
public function addXmlContent(string $content, string $charset = 'UTF-8', int $options = LIBXML_NONET)
{
// remove the default namespace if it's the only namespace to make XPath expressions simpler
if (!preg_match('/xmlns:/', $content)) {
Expand Down Expand Up @@ -318,11 +308,9 @@ public function addNode(\DOMNode $node)
/**
* Returns a node given its position in the node list.
*
* @param int $position The position
*
* @return self
*/
public function eq($position)
public function eq(int $position)
{
if (isset($this->nodes[$position])) {
return $this->createSubCrawler($this->nodes[$position]);
Expand Down Expand Up @@ -360,12 +348,9 @@ public function each(\Closure $closure)
/**
* Slices the list of nodes by $offset and $length.
*
* @param int $offset
* @param int $length
Simperfit marked this conversation as resolved.
Show resolved Hide resolved
*
* @return self
*/
public function slice($offset = 0, $length = null)
public function slice(int $offset = 0, int $length = null)
{
return $this->createSubCrawler(\array_slice($this->nodes, $offset, $length));
}
Expand Down Expand Up @@ -487,8 +472,6 @@ public function parents()
/**
* Returns the children nodes of the current selection.
*
* @param string|null $selector An optional CSS selector to filter children
*
* @return self
*
* @throws \InvalidArgumentException When current node is empty
Expand All @@ -515,13 +498,11 @@ public function children(string $selector = null)
/**
* Returns the attribute value of the first node of the list.
*
* @param string $attribute The attribute name
*
* @return string|null The attribute value or null if the attribute does not exist
*
* @throws \InvalidArgumentException When current node is empty
*/
public function attr($attribute)
public function attr(string $attribute)
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
Expand Down Expand Up @@ -610,11 +591,9 @@ public function html($default = null)
* Since an XPath expression might evaluate to either a simple type or a \DOMNodeList,
* this method will return either an array of simple types or a new Crawler instance.
*
* @param string $xpath An XPath expression
*
* @return array|Crawler An array of evaluation results or a new Crawler instance
*/
public function evaluate($xpath)
public function evaluate(string $xpath)
{
if (null === $this->document) {
throw new \LogicException('Cannot evaluate the expression on an uninitialized crawler.');
Expand Down Expand Up @@ -643,13 +622,10 @@ public function evaluate($xpath)
*
* $crawler->filter('h1 a')->extract(['_text', 'href']);
*
* @param array $attributes An array of attributes
*
* @return array An array of extracted values
*/
public function extract($attributes)
public function extract(array $attributes)
{
$attributes = (array) $attributes;
$count = \count($attributes);

$data = [];
Expand Down Expand Up @@ -679,11 +655,9 @@ public function extract($attributes)
* This means that a child selector "div" or "./div" will match only
* the div elements of the current crawler, not their children.
*
* @param string $xpath An XPath expression
*
* @return self
*/
public function filterXPath($xpath)
public function filterXPath(string $xpath)
{
$xpath = $this->relativize($xpath);

Expand All @@ -700,13 +674,11 @@ public function filterXPath($xpath)
*
* This method only works if you have installed the CssSelector Symfony Component.
*
* @param string $selector A CSS selector
*
* @return self
*
* @throws \RuntimeException if the CssSelector Component is not available
*/
public function filter($selector)
public function filter(string $selector)
{
$converter = $this->createCssSelectorConverter();

Expand All @@ -717,11 +689,9 @@ public function filter($selector)
/**
* Selects links by name or alt value for clickable images.
*
* @param string $value The link text
*
* @return self
*/
public function selectLink($value)
public function selectLink(string $value)
{
return $this->filterRelativeXPath(
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.' '))
Expand All @@ -731,11 +701,9 @@ public function selectLink($value)
/**
* Selects images by alt value.
*
* @param string $value The image alt
*
* @return self A new instance of Crawler with the filtered list of nodes
*/
public function selectImage($value)
public function selectImage(string $value)
{
$xpath = sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', static::xpathLiteral($value));

Expand All @@ -745,11 +713,9 @@ public function selectImage($value)
/**
* Selects a button by name or alt value for images.
*
* @param string $value The button text
*
* @return self
*/
public function selectButton($value)
public function selectButton(string $value)
{
return $this->filterRelativeXPath(
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))
Expand All @@ -759,13 +725,11 @@ public function selectButton($value)
/**
* Returns a Link object for the first node in the list.
*
* @param string $method The method for the link (get by default)
*
* @return Link A Link instance
*
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
*/
public function link($method = 'get')
public function link(string $method = 'get')
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
Expand Down Expand Up @@ -845,14 +809,11 @@ public function images()
/**
* Returns a Form object for the first node in the list.
*
* @param array $values An array of values for the form fields
Simperfit marked this conversation as resolved.
Show resolved Hide resolved
* @param string $method The method for the form
fabpot marked this conversation as resolved.
Show resolved Hide resolved
*
* @return Form A Form instance
*
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
*/
public function form(array $values = null, $method = null)
public function form(array $values = null, string $method = null)
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
Expand All @@ -875,19 +836,13 @@ public function form(array $values = null, $method = null)

/**
* Overloads a default namespace prefix to be used with XPath and CSS expressions.
*
* @param string $prefix
*/
public function setDefaultNamespacePrefix($prefix)
public function setDefaultNamespacePrefix(string $prefix)
{
$this->defaultNamespacePrefix = $prefix;
}

/**
* @param string $prefix
* @param string $namespace
*/
public function registerNamespace($prefix, $namespace)
public function registerNamespace(string $prefix, string $namespace)
{
$this->namespaces[$prefix] = $namespace;
}
Expand All @@ -909,11 +864,9 @@ public function registerNamespace($prefix, $namespace)
* //prints concat('a', "'", 'b"c')
*
*
* @param string $s String to be escaped
*
* @return string Converted string
*/
public static function xpathLiteral($s)
public static function xpathLiteral(string $s)
{
if (false === strpos($s, "'")) {
return sprintf("'%s'", $s);
Expand Down Expand Up @@ -944,11 +897,9 @@ public static function xpathLiteral($s)
*
* The XPath expression should already be processed to apply it in the context of each node.
*
* @param string $xpath
*
* @return self
*/
private function filterRelativeXPath($xpath)
private function filterRelativeXPath(string $xpath)
{
$prefixes = $this->findNamespacePrefixes($xpath);

Expand Down Expand Up @@ -1053,11 +1004,9 @@ private function relativize(string $xpath): string
}

/**
* @param int $position
*
* @return \DOMElement|null
*/
public function getNode($position)
public function getNode(int $position)
{
if (isset($this->nodes[$position])) {
return $this->nodes[$position];
Expand All @@ -1082,11 +1031,10 @@ public function getIterator()

/**
* @param \DOMElement $node
* @param string $siblingDir
*
* @return array
*/
protected function sibling($node, $siblingDir = 'nextSibling')
protected function sibling($node, string $siblingDir = 'nextSibling')
{
$nodes = [];

Expand Down
5 changes: 1 addition & 4 deletions 5 src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,9 @@ private function buildOptionValue(\DOMElement $node): array
/**
* Checks whether given value is in the existing options.
*
* @param string $optionValue
* @param array $options
*
* @return bool
*/
public function containsOption($optionValue, $options)
public function containsOption(string $optionValue, array $options)
{
if ($this->validationDisabled) {
return true;
Expand Down
14 changes: 4 additions & 10 deletions 14 src/Symfony/Component/DomCrawler/Field/FileFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FileFormField extends FormField
*
* @throws \InvalidArgumentException When error code doesn't exist
*/
public function setErrorCode($error)
public function setErrorCode(int $error)
{
$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];
if (!\in_array($error, $codes)) {
Expand All @@ -37,20 +37,16 @@ public function setErrorCode($error)

/**
* Sets the value of the field.
*
* @param string $value The value of the field
*/
public function upload($value)
public function upload(?string $value)
{
$this->setValue($value);
}

/**
* Sets the value of the field.
*
* @param string $value The value of the field
*/
public function setValue($value)
public function setValue(?string $value)
{
if (null !== $value && is_readable($value)) {
$error = UPLOAD_ERR_OK;
Expand Down Expand Up @@ -80,10 +76,8 @@ public function setValue($value)

/**
* Sets path to the file as string for simulating HTTP request.
*
* @param string $path The path to the file
*/
public function setFilePath($path)
public function setFilePath(string $path)
{
parent::setValue($path);
}
Expand Down
4 changes: 1 addition & 3 deletions 4 src/Symfony/Component/DomCrawler/Field/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,8 @@ public function getValue()

/**
* Sets the value of the field.
*
* @param string $value The value of the field
*/
public function setValue($value)
public function setValue(?string $value)
{
$this->value = (string) $value;
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.