Setup PHPStan (PHP Static Analysis Tool)

Require "phpstan/phpstan" for development and add to the Travis CI
configuration file to execute the analysis when Travis executes the
build with the version 7.2 of PHP.

The level of the configuration is very week for now (just "1") and still
quite some changes had to be made in order to make the analysis pass. I
hope it does not take much time to increase the level of the strictness
of the analyses.

I tried to configure that before but because of dependencies with
"symfony/validator" it was not possible.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2018-08-22 18:30:48 +02:00
parent f5c167c411
commit 258a456eec
No known key found for this signature in database
GPG key ID: 221E9281655813A6
19 changed files with 45 additions and 36 deletions

1
.gitignore vendored
View file

@ -2,5 +2,6 @@
.couscous/
composer.lock
Makefile
phpstan.neon
phpunit.xml
vendor/

View file

@ -33,6 +33,10 @@ script:
if [[ "${TRAVIS_PHP_VERSION}" == "7.2" ]]; then
vendor/bin/php-cs-fixer --diff --dry-run --verbose fix
fi
- |
if [[ "${TRAVIS_PHP_VERSION}" == "7.2" ]]; then
vendor/bin/phpstan analyze
fi
after_script:
- |

View file

@ -21,6 +21,7 @@
"friendsofphp/php-cs-fixer": "^2.8",
"malukenho/docheader": "^0.1.4",
"mikey179/vfsStream": "^1.6",
"phpstan/phpstan": "^0.10.3",
"phpunit/phpunit": "^6.4",
"symfony/validator": "^3.0||^4.0",
"zendframework/zend-validator": "^2.0"
@ -41,6 +42,7 @@
},
"autoload-dev": {
"psr-4": {
"Respect\\Validation\\": "tests/unit/",
"Respect\\Validation\\Test\\": "tests/library/"
}
},

View file

@ -80,7 +80,8 @@ class ValidationException extends InvalidArgumentException implements Exception
$this->params = $params;
$this->translator = $translator;
$this->template = $this->chooseTemplate();
$this->message = $this->createMessage();
parent::__construct($this->createMessage());
}
public function getId(): string

View file

@ -38,6 +38,8 @@ class Domain extends AbstractComposite
),
new Not(new EndsWith('-'))
);
parent::__construct();
}
public function tldCheck($do = true)

View file

@ -27,11 +27,11 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator alpha(string $additionalChars = null)
* @method static Validator alwaysInvalid()
* @method static Validator alwaysValid()
* @method static Validator anyOf()
* @method static Validator anyOf(Validatable ...$rule)
* @method static Validator arrayType()
* @method static Validator arrayVal()
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
* @method static Validator base()
* @method static Validator base(int $base, string $chars = null)
* @method static Validator base64()
* @method static Validator between($minimum, $maximum)
* @method static Validator bic(string $countryCode)
@ -92,7 +92,7 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
* @method static Validator keyNested(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
* @method static Validator keySet(Key ...$rule)
* @method static Validator keyValue(string $comparedKey, string $ruleName, string $baseKey)
* @method static Validator languageCode(string $set)
* @method static Validator languageCode(string $set = null)
* @method static Validator leapDate(string $format)
* @method static Validator leapYear()
* @method static Validator length(int $min = null, int $max = null, bool $inclusive = true)

11
phpstan.neon.dist Normal file
View file

@ -0,0 +1,11 @@
parameters:
fileExtensions:
- php
- phpt
ignoreErrors:
- '/Call to an undefined static method Respect\\Validation\\Validator::iDoNotExistSoIShouldThrowException/'
- '/Function Respect\\Validation\\Rules\\is_(file|readable|link|writable) not found/'
level: 1
paths:
- library/
- tests/

View file

@ -2,19 +2,19 @@
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\intValException;
use Respect\Validation\Exceptions\IntValException;
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
try {
v::intVal()->check('42.33');
} catch (intValException $exception) {
} catch (IntValException $exception) {
echo $exception->getMessage().PHP_EOL;
}
try {
v::not(v::intVal())->check(2);
} catch (intValException $exception) {
} catch (IntValException $exception) {
echo $exception->getMessage().PHP_EOL;
}

View file

@ -4,22 +4,15 @@
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Test\Stubs\CountableStub;
use Respect\Validation\Validator as v;
class MyClass
{
public function parse($url)
{
return parse_url($url);
}
}
$input = 'http://www.google.com/search?q=respect.github.com';
try {
v::create()
->call(
[new MyClass(), 'parse'],
[new CountableStub(1), 'count'],
v::arrayVal()->key('scheme', v::startsWith('https'))
)
->assert($input);

View file

@ -52,6 +52,6 @@ final class Stub extends AbstractRule
{
$this->inputs[] = $input;
return (bool) array_shift($validations);
return (bool) array_shift($this->validations);
}
}

View file

@ -11,10 +11,9 @@
declare(strict_types=1);
namespace Respect\Validation\Test\Helpers;
namespace Respect\Validation\Helpers;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Helpers\DateTimeHelper;
/**
* @group helper

View file

@ -11,10 +11,9 @@
declare(strict_types=1);
namespace Respect\Validation\Test\Helpers;
namespace Respect\Validation\Helpers;
use PHPUnit\Framework\TestCase;
use Respect\Validation\Helpers\UndefinedHelper;
use Respect\Validation\Test\DataProvider\UndefinedProvider;
/**

View file

@ -36,7 +36,8 @@ final class CreditCardTest extends RuleTestCase
$message = '"RespectCard" is not a valid credit card brand';
$message .= ' (Available: Any, American Express, Diners Club, Discover, JCB, MasterCard, Visa)';
$this->expectException(ComponentException::class, $message);
$this->expectException(ComponentException::class);
$this->expectExceptionMessage($message);
new CreditCard('RespectCard');
}

View file

@ -47,10 +47,8 @@ class FactorTest extends TestCase
*/
public function invalidFactorShouldThrowFactorException($dividend, $input): void
{
$this->expectException(
FactorException::class,
stringify($input).' must be a factor of '.$dividend
);
$this->expectException(FactorException::class);
$this->expectExceptionMessage(stringify($input).' must be a factor of '.$dividend);
$min = new Factor($dividend);
self::assertFalse($min->__invoke($input));
@ -64,10 +62,8 @@ class FactorTest extends TestCase
*/
public function invalidDividentShouldThrowComponentException($dividend, $input): void
{
$this->expectException(
ComponentException::class,
'Dividend '.stringify($dividend).' must be an integer'
);
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('Dividend '.stringify($dividend).' must be an integer');
// It is enough to simply create a new Factor to trigger the dividend
// exceptions in __construct.
@ -168,7 +164,7 @@ class FactorTest extends TestCase
uniqid('a'),
// Non-scalars.
[],
new \StdClass(),
new \stdClass(),
new \DateTime(),
null,
true,

View file

@ -71,7 +71,7 @@ class FileTest extends TestCase
public function shouldValidateObjects(): void
{
$rule = new File();
$object = $this->createMock('SplFileInfo', ['isFile'], ['somefile.txt']);
$object = $this->createMock('SplFileInfo');
$object->expects(self::once())
->method('isFile')
->will(self::returnValue(true));

View file

@ -59,7 +59,7 @@ class PhpLabelTest extends RuleTestCase
[$rule, 0],
[$rule, 1],
[$rule, []],
[$rule, new \StdClass()],
[$rule, new \stdClass()],
[$rule, new \DateTime()],
];
}

View file

@ -71,7 +71,7 @@ class ReadableTest extends TestCase
public function shouldValidateObjects(): void
{
$rule = new Readable();
$object = $this->createMock('SplFileInfo', ['isReadable'], ['somefile.txt']);
$object = $this->createMock('SplFileInfo');
$object->expects(self::once())
->method('isReadable')
->will(self::returnValue(true));

View file

@ -71,7 +71,7 @@ class SymbolicLinkTest extends TestCase
public function shouldValidateObjects(): void
{
$rule = new SymbolicLink();
$object = $this->createMock('SplFileInfo', ['isLink'], ['somelink.lnk']);
$object = $this->createMock('SplFileInfo');
$object->expects(self::once())
->method('isLink')
->will(self::returnValue(true));

View file

@ -71,7 +71,7 @@ class WritableTest extends TestCase
public function shouldValidateObjects(): void
{
$rule = new Writable();
$object = $this->createMock('SplFileInfo', ['isWritable'], ['somefile.txt']);
$object = $this->createMock('SplFileInfo');
$object->expects(self::once())
->method('isWritable')
->will(self::returnValue(true));