Skip to content

Commit

Permalink
ci: fix PHPStan and PHP CS Fixer (#483)
Browse files Browse the repository at this point in the history
* fix: PHPStan

* fix: php-cs-fixer
  • Loading branch information
dunglas authored Jul 12, 2021
1 parent 66c1e38 commit 4c22ea1
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 78 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/.php_cs.cache
/.php_cs
/.php-cs-fixer.php
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.phar
/composer.lock
Expand Down
14 changes: 1 addition & 13 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

$finder = PhpCsFixer\Finder::create()->in(__DIR__);

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
Expand All @@ -29,25 +29,13 @@
],
'modernize_types_casting' => true,
'native_function_invocation' => ['include' => ['@compiler_optimized'], 'scope' => 'namespaced'],
'no_extra_consecutive_blank_lines' => [
'break',
'continue',
'curly_brace_block',
'extra',
'parenthesis_brace_block',
'return',
'square_brace_block',
'throw',
'use',
],
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => [
'only_untyped' => true,
],
'phpdoc_order' => true,
'psr4' => true,
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
Expand Down
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function request(string $method, string $uri, array $parameters = [], arr

foreach (['parameters', 'files', 'server'] as $arg) {
if ([] !== $$arg) {
throw new \InvalidArgumentException(\sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
throw new \InvalidArgumentException(sprintf('The parameter "$%s" is not supported when using WebDriver.', $arg));
}
}

Expand Down Expand Up @@ -524,7 +524,7 @@ public function get($url): self
$this->start();

// Prepend the base URI to URIs without a host
if (null !== $this->baseUri && (false !== $components = \parse_url($url)) && !isset($components['host'])) {
if (null !== $this->baseUri && (false !== $components = parse_url($url)) && !isset($components['host'])) {
$url = $this->baseUri.$url;
}

Expand Down
8 changes: 4 additions & 4 deletions src/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,19 @@ public function filter($selector): self
public function selectLink($value): self
{
return $this->selectFromXpath(
\sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %1$s) or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %1$s)]]', self::xpathLiteral(' '.$value.' '))
sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %1$s) or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %1$s)]]', self::xpathLiteral(' '.$value.' '))
);
}

public function selectImage($value): self
{
return $this->selectFromXpath(\sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', self::xpathLiteral($value)));
return $this->selectFromXpath(sprintf('descendant-or-self::img[contains(normalize-space(string(@alt)), %s)]', self::xpathLiteral($value)));
}

public function selectButton($value): self
{
return $this->selectFromXpath(
\sprintf(
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")',
self::xpathLiteral(' '.$value.' '),
Expand Down Expand Up @@ -376,7 +376,7 @@ private function createSubCrawlerFromXpath(string $selector, bool $reverse = fal
return $this->createSubCrawler();
}

return $this->createSubCrawler($reverse ? \array_reverse($elements) : $elements);
return $this->createSubCrawler($reverse ? array_reverse($elements) : $elements);
}

private function filterWebDriverBy(WebDriverBy $selector): self
Expand Down
14 changes: 7 additions & 7 deletions src/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function select($value): void
public function tick(): void
{
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
}

$this->setValue(true);
Expand All @@ -73,7 +73,7 @@ public function tick(): void
public function untick(): void
{
if ('checkbox' !== $type = $this->element->getAttribute('type')) {
throw new \LogicException(\sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->element->getAttribute('name'), $type));
}

$this->setValue(false);
Expand All @@ -97,7 +97,7 @@ public function getValue()

$count = \count($value);
if (1 === $count && 'checkbox' === $type) {
return \current($value);
return current($value);
}

return $value;
Expand All @@ -121,7 +121,7 @@ public function setValue($value): void
{
if (\is_bool($value)) {
if ('checkbox' !== $this->type) {
throw new \InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value)));
throw new \InvalidArgumentException(sprintf('Invalid argument of type "%s"', \gettype($value)));
}

if ($value) {
Expand Down Expand Up @@ -196,12 +196,12 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName && 'select' !== $tagName) {
throw new \LogicException(\sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $tagName));
}

$type = \strtolower((string) $this->element->getAttribute('type'));
$type = strtolower((string) $this->element->getAttribute('type'));
if ('input' === $tagName && 'checkbox' !== $type && 'radio' !== $type) {
throw new \LogicException(\sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type));
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $type));
}

$this->type = 'select' === $tagName ? 'select' : $type;
Expand Down
20 changes: 10 additions & 10 deletions src/DomCrawler/Field/FileFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function setValue($value): void
{
$value = $this->sanitizeValue($value);

if (null !== $value && \is_readable($value)) {
if (null !== $value && is_readable($value)) {
$error = \UPLOAD_ERR_OK;
$size = \filesize($value);
$name = \pathinfo($value, \PATHINFO_BASENAME);
$size = filesize($value);
$name = pathinfo($value, \PATHINFO_BASENAME);

$this->setFilePath($value);
$value = $this->element->getAttribute('value');
Expand Down Expand Up @@ -72,12 +72,12 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName) {
throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $tagName));
}

$type = \strtolower($this->element->getAttribute('type'));
$type = strtolower($this->element->getAttribute('type'));
if ('file' !== $type) {
throw new \LogicException(\sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $type));
}

$value = $this->element->getAttribute('value');
Expand All @@ -95,16 +95,16 @@ private function setValueFromTmp(string $tmpValue): void
// size not determinable
$size = 0;
// C:\fakepath\filename.extension
$basename = \pathinfo($value, \PATHINFO_BASENAME);
$nameParts = \explode('\\', $basename);
$name = \end($nameParts);
$basename = pathinfo($value, \PATHINFO_BASENAME);
$nameParts = explode('\\', $basename);
$name = end($nameParts);

$this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size];
}

private function sanitizeValue(?string $value): ?string
{
$realpathValue = \is_string($value) && $value ? \realpath($value) : false;
$realpathValue = \is_string($value) && $value ? realpath($value) : false;
if (\is_string($realpathValue)) {
$value = $realpathValue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Field/FormFieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function setTextValue($value): void
}

$existingValueLength = \strlen($v);
$deleteKeys = \str_repeat(WebDriverKeys::BACKSPACE.WebDriverKeys::DELETE, $existingValueLength);
$deleteKeys = str_repeat(WebDriverKeys::BACKSPACE.WebDriverKeys::DELETE, $existingValueLength);
$this->element->sendKeys($deleteKeys.$value);
}
}
4 changes: 2 additions & 2 deletions src/DomCrawler/Field/InputFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('input' !== $tagName && 'button' !== $tagName) {
throw new \LogicException(\sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
throw new \LogicException(sprintf('An InputFormField can only be created from an input or button tag (%s given).', $tagName));
}

$type = \strtolower((string) $this->element->getAttribute('type'));
$type = strtolower((string) $this->element->getAttribute('type'));
if ('checkbox' === $type) {
throw new \LogicException('Checkboxes should be instances of ChoiceFormField.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Field/TextareaFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function initialize(): void
{
$tagName = $this->element->getTagName();
if ('textarea' !== $tagName) {
throw new \LogicException(\sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
throw new \LogicException(sprintf('A TextareaFormField can only be created from a textarea tag (%s given).', $tagName));
}
}
}
16 changes: 8 additions & 8 deletions src/DomCrawler/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ private function setElement(WebDriverElement $element): void
{
$this->button = $element;
$tagName = $element->getTagName();
if ('button' === $tagName || ('input' === $tagName && \in_array(\strtolower($element->getAttribute('type')), ['submit', 'button', 'image'], true))) {
if ('button' === $tagName || ('input' === $tagName && \in_array(strtolower($element->getAttribute('type')), ['submit', 'button', 'image'], true))) {
if (null !== $formId = $element->getAttribute('form')) {
// if the node has the HTML5-compliant 'form' attribute, use it
try {
$form = $this->webDriver->findElement(WebDriverBy::id($formId));
} catch (NoSuchElementException $e) {
throw new \LogicException(\sprintf('The selected node has an invalid form attribute (%s).', $formId));
throw new \LogicException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
}

$this->element = $form;
Expand All @@ -82,7 +82,7 @@ private function setElement(WebDriverElement $element): void
}
} while ('form' !== $element->getTagName());
} elseif ('form' !== $tagName = $element->getTagName()) {
throw new \LogicException(\sprintf('Unable to submit on a "%s" tag.', $tagName));
throw new \LogicException(sprintf('Unable to submit on a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down Expand Up @@ -132,10 +132,10 @@ public function getValues(): array

$value = $this->getValue($element);

$isArrayElement = \is_array($value) && '[]' === \substr($name, -2);
$isArrayElement = \is_array($value) && '[]' === substr($name, -2);
if ($isArrayElement) {
// compatibility with the DomCrawler API
$name = \substr($name, 0, -2);
$name = substr($name, 0, -2);
}

if ('checkbox' === $type) {
Expand Down Expand Up @@ -185,10 +185,10 @@ public function getMethod(): string

// If the form was created from a button rather than the form node, check for HTML5 method override
if ($this->button !== $this->element && null !== $this->button->getAttribute('formmethod')) {
return \strtoupper($this->button->getAttribute('formmethod'));
return strtoupper($this->button->getAttribute('formmethod'));
}

return $this->element->getAttribute('method') ? \strtoupper($this->element->getAttribute('method')) : 'GET';
return $this->element->getAttribute('method') ? strtoupper($this->element->getAttribute('method')) : 'GET';
}

public function has($name): bool
Expand Down Expand Up @@ -263,7 +263,7 @@ protected function getRawUri(): string
private function getFormElement(string $name): WebDriverElement
{
return $this->element->findElement(WebDriverBy::xpath(
\sprintf('.//input[@name=%1$s] | .//textarea[@name=%1$s] | .//select[@name=%1$s] | .//button[@name=%1$s] | .//input[@name=%2$s] | .//textarea[@name=%2$s] | .//select[@name=%2$s] | .//button[@name=%2$s]', XPathEscaper::escapeQuotes($name), XPathEscaper::escapeQuotes($name.'[]'))
sprintf('.//input[@name=%1$s] | .//textarea[@name=%1$s] | .//select[@name=%1$s] | .//button[@name=%1$s] | .//input[@name=%2$s] | .//textarea[@name=%2$s] | .//select[@name=%2$s] | .//button[@name=%2$s]', XPathEscaper::escapeQuotes($name), XPathEscaper::escapeQuotes($name.'[]'))
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Image extends BaseImage
public function __construct(WebDriverElement $element)
{
if ('img' !== $tagName = $element->getTagName()) {
throw new \LogicException(\sprintf('Unable to visualize a "%s" tag.', $tagName));
throw new \LogicException(sprintf('Unable to visualize a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down
2 changes: 1 addition & 1 deletion src/DomCrawler/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(WebDriverElement $element, string $currentUri)
{
$tagName = $element->getTagName();
if ('a' !== $tagName && 'area' !== $tagName && 'link' !== $tagName) {
throw new \LogicException(\sprintf('Unable to navigate from a "%s" tag.', $tagName));
throw new \LogicException(sprintf('Unable to navigate from a "%s" tag.', $tagName));
}

$this->element = $element;
Expand Down
2 changes: 1 addition & 1 deletion src/ExceptionThrower.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ trait ExceptionThrower
{
private function createNotSupportedException(string $method): \InvalidArgumentException
{
return new \InvalidArgumentException(\sprintf('The "%s" method is not supported when using WebDriver.', $method));
return new \InvalidArgumentException(sprintf('The "%s" method is not supported when using WebDriver.', $method));
}
}
2 changes: 1 addition & 1 deletion src/PantherTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

if (\class_exists(WebTestCase::class)) {
if (class_exists(WebTestCase::class)) {
abstract class PantherTestCase extends WebTestCase
{
use WebTestAssertionsTrait;
Expand Down
8 changes: 4 additions & 4 deletions src/PantherTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ public static function isWebServerStarted(): bool

public function takeScreenshotIfTestFailed(): void
{
if (!in_array($this->getStatus(), [BaseTestRunner::STATUS_ERROR, BaseTestRunner::STATUS_FAILURE])) {
if (!\in_array($this->getStatus(), [BaseTestRunner::STATUS_ERROR, BaseTestRunner::STATUS_FAILURE], true)) {
return;
}

$type = $this->getStatus() === BaseTestRunner::STATUS_FAILURE ? 'failure' : 'error';
$type = BaseTestRunner::STATUS_FAILURE === $this->getStatus() ? 'failure' : 'error';
$test = $this->toString();

ServerExtension::takeScreenshots($type, $test);
Expand Down Expand Up @@ -186,7 +186,7 @@ protected static function createPantherClient(array $options = [], array $kernel
self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri);
}

if (\is_a(self::class, KernelTestCase::class, true)) {
if (is_a(self::class, KernelTestCase::class, true)) {
static::bootKernel($kernelOptions); // @phpstan-ignore-line
}

Expand Down Expand Up @@ -224,7 +224,7 @@ protected static function createHttpBrowserClient(array $options = [], array $ke
self::$httpBrowserClient = new HttpBrowserClient(HttpClient::create());
}

if (\is_a(self::class, KernelTestCase::class, true)) {
if (is_a(self::class, KernelTestCase::class, true)) {
static::bootKernel($kernelOptions); // @phpstan-ignore-line
}

Expand Down
2 changes: 1 addition & 1 deletion src/ProcessManager/WebServerReadinessProbeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private function checkPortAvailable(string $hostname, int $port, bool $throw = t
if (\is_resource($resource)) {
fclose($resource);
if ($throw) {
throw new \RuntimeException(\sprintf('The port %d is already in use.', $port));
throw new \RuntimeException(sprintf('The port %d is already in use.', $port));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class ServerExtension implements BeforeFirstTestHook, AfterLastTestHook, B

public static function registerClient(Client $client): void
{
if (self::$enabled && !in_array($client, self::$registeredClients, true)) {
if (self::$enabled && !\in_array($client, self::$registeredClients, true)) {
self::$registeredClients[] = $client;
}
}
Expand Down
Loading

0 comments on commit 4c22ea1

Please sign in to comment.