Skip to content

Commit bc34931

Browse files
authored
Merge pull request #47 from dunglas/phpstan
Analyse with PHPStan. Fix bugs.
2 parents eeba22d + 2150bc2 commit bc34931

File tree

13 files changed

+180
-132
lines changed

13 files changed

+180
-132
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ cache:
1010
- $HOME/.composer/cache
1111

1212
before_install:
13-
- wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.11.1/php-cs-fixer.phar
13+
- wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.12.1/php-cs-fixer.phar
14+
- wget https://github.com/phpstan/phpstan/releases/download/0.10.1/phpstan.phar
1415

1516
before_script:
1617
- phpenv config-rm xdebug.ini
@@ -19,3 +20,4 @@ before_script:
1920
script:
2021
- phpunit
2122
- php php-cs-fixer.phar fix --dry-run --diff --no-ansi
23+
- php phpstan.phar analyse src tests --level=6

phpstan.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
ignoreErrors:
3+
# https://github.com/symfony/symfony/pull/27849
4+
- '/Parameter #1 \$value of method Symfony\\Component\\DomCrawler\\Field\\ChoiceFormField::select\(\) expects string, array<int, string> given\./'
5+
# Require a redesign of the underlying Symfony components
6+
- '#Panthere\\[a-zA-Z\\]+::__construct\(\) does not call parent constructor from Symfony\\Component\\(BrowserKit|DomCrawler)\\[a-zA-Z]+\.#'

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class Client extends BaseClient implements WebDriver
3838
use ExceptionThrower;
3939

4040
/**
41-
* @var WebDriver
41+
* @var WebDriver|null
4242
*/
4343
private $webDriver;
4444
private $browserManager;

src/DomCrawler/Crawler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
use Facebook\WebDriver\WebDriver;
1818
use Facebook\WebDriver\WebDriverBy;
1919
use Facebook\WebDriver\WebDriverElement;
20+
use Panthere\ExceptionThrower;
2021
use Symfony\Component\DomCrawler\Crawler as BaseCrawler;
2122

2223
/**
2324
* @author Kévin Dunglas <[email protected]>
2425
*/
2526
final class Crawler extends BaseCrawler implements WebDriverElement
2627
{
28+
use ExceptionThrower;
29+
2730
private $elements;
2831
private $webDriver;
2932

src/DomCrawler/Field/ChoiceFormField.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ public function untick()
7878
/**
7979
* Sets the value of the field.
8080
*
81-
* @param string $value The value of the field
81+
* @param string|array|bool $value The value of the field
8282
*
8383
* @throws \InvalidArgumentException When value type provided is not correct
8484
*/
8585
public function setValue($value)
8686
{
87-
if ('checkbox' === $this->type && \is_bool($value)) {
87+
if (\is_bool($value)) {
88+
if ('checkbox' !== $this->type) {
89+
throw new \InvalidArgumentException(\sprintf('Invalid argument of type "%s"', \gettype($value)));
90+
}
91+
8892
if ($value) {
8993
if (!$this->element->isSelected()) {
9094
$this->element->click();

src/DomCrawler/Form.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getValues()
126126
}
127127

128128
// Flatten non array-checkboxes
129-
if (!$isArrayElement && 1 === \count($value)) {
129+
if (\is_array($value) && !$isArrayElement && 1 === \count($value)) {
130130
$value = $value[0];
131131
}
132132
}
@@ -300,10 +300,12 @@ private function setValue(string $name, $value): void
300300
try {
301301
$element = $this->element->findElement(WebDriverBy::name($name));
302302
} catch (NoSuchElementException $e) {
303-
// Compatibility with the DomCrawler API
304-
if (\is_array($value)) {
305-
$element = $this->element->findElement(WebDriverBy::name($name.'[]'));
303+
if (!\is_array($value)) {
304+
throw $e;
306305
}
306+
307+
// Compatibility with the DomCrawler API
308+
$element = $this->element->findElement(WebDriverBy::name($name.'[]'));
307309
}
308310

309311
if (null === $webDriverSelect = $this->getWebDriverSelect($element)) {

src/PanthereTestCase.php

Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -13,119 +13,17 @@
1313

1414
namespace Panthere;
1515

16-
use Goutte\Client as GoutteClient;
17-
use GuzzleHttp\Client as GuzzleClient;
18-
use Panthere\Client as PanthereClient;
19-
use Panthere\ProcessManager\WebServerManager;
2016
use PHPUnit\Framework\TestCase;
2117
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
2218

2319
if (\class_exists(WebTestCase::class)) {
24-
/**
25-
* @internal
26-
*/
27-
abstract class InternalTestCase extends WebTestCase
20+
abstract class PanthereTestCase extends WebTestCase
2821
{
22+
use PanthereTestCaseTrait;
2923
}
3024
} else {
31-
/**
32-
* @internal
33-
*/
34-
abstract class InternalTestCase extends TestCase
25+
abstract class PanthereTestCase extends TestCase
3526
{
36-
}
37-
}
38-
39-
/**
40-
* @author Kévin Dunglas <[email protected]>
41-
*/
42-
abstract class PanthereTestCase extends InternalTestCase
43-
{
44-
/**
45-
* @var string|null
46-
*/
47-
protected static $webServerDir;
48-
49-
/**
50-
* @var WebServerManager|null
51-
*/
52-
protected static $webServerManager;
53-
54-
/**
55-
* @var string|null
56-
*/
57-
protected static $baseUri;
58-
59-
/**
60-
* @var GoutteClient|null
61-
*/
62-
protected static $goutteClient;
63-
64-
/**
65-
* @var PanthereClient|null
66-
*/
67-
protected static $panthereClient;
68-
69-
public static function tearDownAfterClass()
70-
{
71-
if (null !== self::$webServerManager) {
72-
self::$webServerManager->quit();
73-
self::$webServerManager = null;
74-
}
75-
76-
if (null !== self::$panthereClient) {
77-
self::$panthereClient->quit();
78-
self::$panthereClient = null;
79-
}
80-
81-
if (null !== self::$goutteClient) {
82-
self::$goutteClient = null;
83-
}
84-
85-
self::$baseUri = null;
86-
}
87-
88-
protected static function startWebServer(?string $webServerDir = null, string $hostname = '127.0.0.1', int $port = 9000): void
89-
{
90-
if (null !== static::$webServerManager) {
91-
return;
92-
}
93-
94-
if (null === $webServerDir) {
95-
// Try the local $webServerDir property, or the PANTHERE_WEB_SERVER_DIR env var or default to the Flex directory structure
96-
$webServerDir = static::$webServerDir ?? $_ENV['PANTHERE_WEB_SERVER_DIR'] ?? __DIR__.'/../../../../public';
97-
}
98-
99-
self::$webServerManager = new WebServerManager($webServerDir, $hostname, $port);
100-
self::$webServerManager->start();
101-
102-
self::$baseUri = "http://$hostname:$port";
103-
}
104-
105-
protected static function createPanthereClient(string $hostname = '127.0.0.1', int $port = 9000): PanthereClient
106-
{
107-
self::startWebServer(null, $hostname, $port);
108-
if (null === self::$panthereClient) {
109-
self::$panthereClient = Client::createChromeClient(null, null, [], self::$baseUri);
110-
}
111-
112-
return self::$panthereClient;
113-
}
114-
115-
protected static function createGoutteClient(): GoutteClient
116-
{
117-
if (!\class_exists(GoutteClient::class)) {
118-
throw new \RuntimeException('Goutte is not installed. Run "composer req fabpot/goutte".');
119-
}
120-
121-
self::startWebServer();
122-
if (null === self::$goutteClient) {
123-
$goutteClient = new GoutteClient();
124-
$goutteClient->setClient(new GuzzleClient(['base_uri' => self::$baseUri]));
125-
126-
self::$goutteClient = $goutteClient;
127-
}
128-
129-
return self::$goutteClient;
27+
use PanthereTestCaseTrait;
13028
}
13129
}

src/PanthereTestCaseTrait.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Panthère project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Panthere;
15+
16+
use Goutte\Client as GoutteClient;
17+
use GuzzleHttp\Client as GuzzleClient;
18+
use Panthere\Client as PanthereClient;
19+
use Panthere\ProcessManager\WebServerManager;
20+
21+
/**
22+
* Eases conditional class definition.
23+
*
24+
* @internal
25+
*
26+
* @author Kévin Dunglas <[email protected]>
27+
*/
28+
trait PanthereTestCaseTrait
29+
{
30+
/**
31+
* @var string|null
32+
*/
33+
protected static $webServerDir;
34+
35+
/**
36+
* @var WebServerManager|null
37+
*/
38+
protected static $webServerManager;
39+
40+
/**
41+
* @var string|null
42+
*/
43+
protected static $baseUri;
44+
45+
/**
46+
* @var GoutteClient|null
47+
*/
48+
protected static $goutteClient;
49+
50+
/**
51+
* @var PanthereClient|null
52+
*/
53+
protected static $panthereClient;
54+
55+
public static function tearDownAfterClass()
56+
{
57+
if (null !== self::$webServerManager) {
58+
self::$webServerManager->quit();
59+
self::$webServerManager = null;
60+
}
61+
62+
if (null !== self::$panthereClient) {
63+
self::$panthereClient->quit();
64+
self::$panthereClient = null;
65+
}
66+
67+
if (null !== self::$goutteClient) {
68+
self::$goutteClient = null;
69+
}
70+
71+
self::$baseUri = null;
72+
}
73+
74+
protected static function startWebServer(?string $webServerDir = null, string $hostname = '127.0.0.1', int $port = 9000): void
75+
{
76+
if (null !== static::$webServerManager) {
77+
return;
78+
}
79+
80+
if (null === $webServerDir) {
81+
// Try the local $webServerDir property, or the PANTHERE_WEB_SERVER_DIR env var or default to the Flex directory structure
82+
$webServerDir = static::$webServerDir ?? $_ENV['PANTHERE_WEB_SERVER_DIR'] ?? __DIR__.'/../../../../public';
83+
}
84+
85+
self::$webServerManager = new WebServerManager($webServerDir, $hostname, $port);
86+
self::$webServerManager->start();
87+
88+
self::$baseUri = "http://$hostname:$port";
89+
}
90+
91+
protected static function createPanthereClient(string $hostname = '127.0.0.1', int $port = 9000): PanthereClient
92+
{
93+
self::startWebServer(null, $hostname, $port);
94+
if (null === self::$panthereClient) {
95+
self::$panthereClient = Client::createChromeClient(null, null, [], self::$baseUri);
96+
}
97+
98+
return self::$panthereClient;
99+
}
100+
101+
protected static function createGoutteClient(): GoutteClient
102+
{
103+
if (!\class_exists(GoutteClient::class)) {
104+
throw new \RuntimeException('Goutte is not installed. Run "composer req fabpot/goutte".');
105+
}
106+
107+
self::startWebServer();
108+
if (null === self::$goutteClient) {
109+
$goutteClient = new GoutteClient();
110+
$goutteClient->setClient(new GuzzleClient(['base_uri' => self::$baseUri]));
111+
112+
self::$goutteClient = $goutteClient;
113+
}
114+
115+
return self::$goutteClient;
116+
}
117+
}

src/ProcessManager/ChromeManager.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public function __construct(?string $chromeDriverBinary = null, ?array $argument
3838
}
3939

4040
/**
41-
* @param string[]|null $arguments
42-
*
4341
* @throws \RuntimeException
4442
*/
4543
public function start(): WebDriver

0 commit comments

Comments
 (0)