Skip to content
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

Add types for method parameters #171

Merged
merged 1 commit into from
Jun 15, 2023
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": ">=7.2",
"ext-dom": "*",
"behat/mink": "^1.9.0@dev",
"behat/mink": "^1.11.0@dev",
"symfony/browser-kit": "^4.4 || ^5.0 || ^6.0",
"symfony/dom-crawler": "^4.4 || ^5.0 || ^6.0"
},
Expand Down
58 changes: 29 additions & 29 deletions src/BrowserKitDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function reset()
/**
* {@inheritdoc}
*/
public function visit($url)
public function visit(string $url)
{
$this->client->request('GET', $this->prepareUrl($url), array(), array(), $this->serverParameters);
$this->forms = array();
Expand Down Expand Up @@ -173,7 +173,7 @@ public function back()
/**
* {@inheritdoc}
*/
public function setBasicAuth($user, $password)
public function setBasicAuth($user, string $password)
{
if (false === $user) {
unset($this->serverParameters['PHP_AUTH_USER'], $this->serverParameters['PHP_AUTH_PW']);
Expand All @@ -190,7 +190,7 @@ public function setBasicAuth($user, $password)
/**
* {@inheritdoc}
*/
public function setRequestHeader($name, $value)
public function setRequestHeader(string $name, string $value)
{
$contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true);
$name = str_replace('-', '_', strtoupper($name));
Expand All @@ -214,7 +214,7 @@ public function getResponseHeaders()
/**
* {@inheritdoc}
*/
public function setCookie($name, $value = null)
public function setCookie(string $name, ?string $value = null)
{
if (null === $value) {
$this->deleteCookie($name);
Expand Down Expand Up @@ -266,7 +266,7 @@ private function getCookiePath(): string
/**
* {@inheritdoc}
*/
public function getCookie($name)
public function getCookie(string $name)
{
// Note that the following doesn't work well because
// Symfony\Component\BrowserKit\CookieJar stores cookies by name,
Expand Down Expand Up @@ -309,7 +309,7 @@ public function getContent()
/**
* {@inheritdoc}
*/
public function findElementXpaths($xpath)
public function findElementXpaths(string $xpath)
{
$nodes = $this->getCrawler()->filterXPath($xpath);

Expand All @@ -324,15 +324,15 @@ public function findElementXpaths($xpath)
/**
* {@inheritdoc}
*/
public function getTagName($xpath)
public function getTagName(string $xpath)
{
return $this->getCrawlerNode($this->getFilteredCrawler($xpath))->nodeName;
}

/**
* {@inheritdoc}
*/
public function getText($xpath)
public function getText(string $xpath)
{
$text = $this->getFilteredCrawler($xpath)->text(null, true);

Expand All @@ -342,15 +342,15 @@ public function getText($xpath)
/**
* {@inheritdoc}
*/
public function getHtml($xpath)
public function getHtml(string $xpath)
{
return $this->getFilteredCrawler($xpath)->html();
}

/**
* {@inheritdoc}
*/
public function getOuterHtml($xpath)
public function getOuterHtml(string $xpath)
{
$crawler = $this->getFilteredCrawler($xpath);

Expand All @@ -360,7 +360,7 @@ public function getOuterHtml($xpath)
/**
* {@inheritdoc}
*/
public function getAttribute($xpath, $name)
public function getAttribute(string $xpath, string $name)
{
$node = $this->getFilteredCrawler($xpath);

Expand All @@ -374,7 +374,7 @@ public function getAttribute($xpath, $name)
/**
* {@inheritdoc}
*/
public function getValue($xpath)
public function getValue(string $xpath)
{
if (in_array($this->getAttribute($xpath, 'type'), array('submit', 'image', 'button'), true)) {
return $this->getAttribute($xpath, 'value');
Expand Down Expand Up @@ -406,7 +406,7 @@ public function getValue($xpath)
/**
* {@inheritdoc}
*/
public function setValue($xpath, $value)
public function setValue(string $xpath, $value)
{
$field = $this->getFormField($xpath);

Expand All @@ -433,23 +433,23 @@ public function setValue($xpath, $value)
/**
* {@inheritdoc}
*/
public function check($xpath)
public function check(string $xpath)
{
$this->getCheckboxField($xpath)->tick();
}

/**
* {@inheritdoc}
*/
public function uncheck($xpath)
public function uncheck(string $xpath)
{
$this->getCheckboxField($xpath)->untick();
}

/**
* {@inheritdoc}
*/
public function selectOption($xpath, $value, $multiple = false)
public function selectOption(string $xpath, string $value, bool $multiple = false)
{
$field = $this->getFormField($xpath);

Expand All @@ -469,7 +469,7 @@ public function selectOption($xpath, $value, $multiple = false)
/**
* {@inheritdoc}
*/
public function isSelected($xpath)
public function isSelected(string $xpath)
{
$optionValue = $this->getOptionValue($this->getCrawlerNode($this->getFilteredCrawler($xpath)));
$selectField = $this->getFormField('(' . $xpath . ')/ancestor-or-self::*[local-name()="select"]');
Expand All @@ -481,7 +481,7 @@ public function isSelected($xpath)
/**
* {@inheritdoc}
*/
public function click($xpath)
public function click(string $xpath)
{
$crawler = $this->getFilteredCrawler($xpath);
$node = $this->getCrawlerNode($crawler);
Expand All @@ -504,7 +504,7 @@ public function click($xpath)
/**
* {@inheritdoc}
*/
public function isChecked($xpath)
public function isChecked(string $xpath)
{
$field = $this->getFormField($xpath);

Expand All @@ -524,7 +524,7 @@ public function isChecked($xpath)
/**
* {@inheritdoc}
*/
public function attachFile($xpath, $path)
public function attachFile(string $xpath, string $path)
{
$field = $this->getFormField($xpath);

Expand All @@ -538,7 +538,7 @@ public function attachFile($xpath, $path)
/**
* {@inheritdoc}
*/
public function submitForm($xpath)
public function submitForm(string $xpath)
{
$crawler = $this->getFilteredCrawler($xpath);

Expand Down Expand Up @@ -574,7 +574,7 @@ protected function getResponse()
*
* @return string
*/
protected function prepareUrl($url)
protected function prepareUrl(string $url)
{
return $url;
}
Expand All @@ -589,7 +589,7 @@ protected function prepareUrl($url)
* @throws DriverException
* @throws \InvalidArgumentException when the field does not exist in the BrowserKit form
*/
protected function getFormField($xpath)
protected function getFormField(string $xpath)
{
$fieldNode = $this->getCrawlerNode($this->getFilteredCrawler($xpath));
$fieldName = str_replace('[]', '', $fieldNode->getAttribute('name'));
Expand Down Expand Up @@ -621,7 +621,7 @@ protected function getFormField($xpath)
*
* @throws DriverException when the field is not a checkbox
*/
private function getCheckboxField($xpath)
private function getCheckboxField(string $xpath): ChoiceFormField
{
$field = $this->getFormField($xpath);

Expand All @@ -639,7 +639,7 @@ private function getCheckboxField($xpath)
*
* @throws DriverException if the form node cannot be found
*/
private function getFormNode(\DOMElement $element)
private function getFormNode(\DOMElement $element): \DOMElement
{
if ($element->hasAttribute('form')) {
$formId = $element->getAttribute('form');
Expand Down Expand Up @@ -746,7 +746,7 @@ private function canResetForm(\DOMElement $node): bool
*
* @return string
*/
private function getFormNodeId(\DOMElement $form)
private function getFormNodeId(\DOMElement $form): string
{
return md5($form->getLineNo() . $form->getNodePath() . $form->nodeValue);
}
Expand All @@ -760,7 +760,7 @@ private function getFormNodeId(\DOMElement $form)
*
* @see \Symfony\Component\DomCrawler\Field\ChoiceFormField::buildOptionValue
*/
private function getOptionValue(\DOMElement $option)
private function getOptionValue(\DOMElement $option): string
{
if ($option->hasAttribute('value')) {
return $option->getAttribute('value');
Expand Down Expand Up @@ -829,7 +829,7 @@ private function getCrawlerNode(Crawler $crawler): \DOMElement
*
* @throws DriverException when no matching elements are found
*/
private function getFilteredCrawler($xpath)
private function getFilteredCrawler(string $xpath): Crawler
{
if (!count($crawler = $this->getCrawler()->filterXPath($xpath))) {
throw new DriverException(sprintf('There is no element matching XPath "%s"', $xpath));
Expand All @@ -845,7 +845,7 @@ private function getFilteredCrawler($xpath)
*
* @throws DriverException
*/
private function getCrawler()
private function getCrawler(): Crawler
{
try {
$crawler = $this->client->getCrawler();
Expand Down