Skip to content

Commit

Permalink
Merge pull request #18 from lookyman/phpstan-0.12
Browse files Browse the repository at this point in the history
Bump PHPStan dependency to 0.12
  • Loading branch information
Ocramius committed Jan 28, 2020
2 parents f0bfc94 + 497505e commit 00cd927
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 56 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,3 @@ jobs:

- stage: Code Style
script: vendor/bin/phpcs

- stage: Dependencies Check
script: vendor/bin/composer-require-checker

- stage: Backwards Compatibility Check
script: vendor/bin/roave-backward-compatibility-check
8 changes: 3 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
"require": {
"php": "^7.2",
"nikic/php-parser": "^4.0",
"phpstan/phpstan": "^0.11.7"
"phpstan/phpstan": "^0.12"
},
"require-dev": {
"doctrine/coding-standard": "^6.0",
"infection/infection": "^0.11.4",
"maglnet/composer-require-checker": "^1.1",
"phpstan/phpstan-phpunit": "^0.11",
"phpstan/phpstan-strict-rules": "^0.11",
"phpstan/phpstan-phpunit": "^0.12",
"phpstan/phpstan-strict-rules": "^0.12",
"phpunit/phpunit": "^7.4",
"roave/backward-compatibility-check": "^2.0",
"squizlabs/php_codesniffer": "^3.4"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ parameters:
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests/src
level: 7
level: 8

services:
scopeIsInClass:
Expand Down
10 changes: 7 additions & 3 deletions src/DisallowFloatAssignedToVariableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node>
*/
final class DisallowFloatAssignedToVariableRule implements Rule
{
/** @var Standard */
Expand All @@ -27,7 +31,7 @@ public function getNodeType() : string
}

/**
* @return string[]
* {@inheritDoc}
*/
public function processNode(Node $node, Scope $scope) : array
{
Expand All @@ -41,11 +45,11 @@ public function processNode(Node $node, Scope $scope) : array
}

return [
sprintf(
RuleErrorBuilder::message(sprintf(
'Cannot assign %s to %s - floats are not allowed.',
$resultType->describe(VerbosityLevel::typeOnly()),
$this->printer->prettyPrintExpr($node->var)
),
))->build(),
];
}
}
13 changes: 7 additions & 6 deletions src/DisallowFloatEverywhereRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function assert;
use function sprintf;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr>
*/
class DisallowFloatEverywhereRule implements Rule
{
public function getNodeType() : string
Expand All @@ -20,7 +23,7 @@ public function getNodeType() : string
}

/**
* @return string[]
* {@inheritDoc}
*/
public function processNode(Node $node, Scope $scope) : array
{
Expand All @@ -30,18 +33,16 @@ public function processNode(Node $node, Scope $scope) : array
return [];
}

assert($node instanceof Expr);

$nodeType = $scope->getType($node);
if (! FloatTypeHelper::isFloat($nodeType)) {
return [];
}

return [
sprintf(
RuleErrorBuilder::message(sprintf(
'Cannot have %s as a result type of this expression - floats are not allowed.',
$nodeType->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}
}
34 changes: 18 additions & 16 deletions src/DisallowFloatInFunctionSignatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
use PHPStan\Broker\Broker;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function array_filter;
use function array_keys;
use function array_map;
use function array_merge;
use function array_values;
use function assert;
use function sprintf;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Function_>
*/
final class DisallowFloatInFunctionSignatureRule implements Rule
{
/** @var Broker */
Expand All @@ -38,18 +42,16 @@ public function getNodeType() : string
}

/**
* @return string[]
* {@inheritDoc}
*/
public function processNode(Node $node, Scope $scope) : array
{
assert($node instanceof Function_);

$functionName = new Name($node->name->toString());
if (! $this->broker->hasCustomFunction($functionName, $scope)) {
if (! $this->broker->hasFunction($functionName, $scope)) {
return [];
}

$functionReflection = $this->broker->getCustomFunction($functionName, $scope);
$functionReflection = $this->broker->getFunction($functionName, $scope);

$errors = [];

Expand All @@ -61,43 +63,43 @@ public function processNode(Node $node, Scope $scope) : array
return array_filter(array_merge([], ...$errors));
}

/** @return string[] */
/** @return RuleError[] */
private function returnTypeViolations(
ParametersAcceptorWithPhpDocs $function,
ParametersAcceptor $function,
FunctionReflection $functionReflection
) : array {
if (! FloatTypeHelper::isFloat($function->getReturnType())) {
return [];
}

return [sprintf(
return [RuleErrorBuilder::message(sprintf(
'Function %s() cannot have %s as its return type - floats are not allowed.',
$functionReflection->getName(),
$function->getReturnType()->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}

/** @return string[]|null[] */
/** @return RuleError[]|null[] */
private function violationsForParameters(
ParametersAcceptorWithPhpDocs $function,
ParametersAcceptor $function,
FunctionReflection $functionReflection
) : array {
$parameters = $function->getParameters();

return array_map(
static function (ParameterReflection $parameter, int $index) use ($functionReflection) : ?string {
static function (ParameterReflection $parameter, int $index) use ($functionReflection) : ?RuleError {
if (! FloatTypeHelper::isFloat($parameter->getType())) {
return null;
}

return sprintf(
return RuleErrorBuilder::message(sprintf(
'Parameter #%d $%s of function %s() cannot have %s as its type - floats are not allowed.',
$index + 1,
$parameter->getName(),
$functionReflection->getName(),
$parameter->getType()->describe(VerbosityLevel::typeOnly())
);
))->build();
},
array_values($parameters),
array_keys($parameters)
Expand Down
24 changes: 13 additions & 11 deletions src/DisallowFloatInMethodSignatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\VerbosityLevel;
use function array_filter;
use function array_keys;
use function array_map;
use function array_merge;
use function array_values;
use function assert;
use function sprintf;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\ClassMethod>
*/
final class DisallowFloatInMethodSignatureRule implements Rule
{
public function getNodeType() : string
Expand All @@ -29,12 +33,10 @@ public function getNodeType() : string
}

/**
* @return string[]
* {@inheritDoc}
*/
public function processNode(Node $node, Scope $scope) : array
{
assert($node instanceof ClassMethod);

if (! $scope->isInClass()) {
throw new ShouldNotHappenException();
}
Expand All @@ -53,7 +55,7 @@ public function processNode(Node $node, Scope $scope) : array
return array_filter(array_merge([], ...$errors));
}

/** @return string[] */
/** @return RuleError[] */
private function returnTypeViolations(
ParametersAcceptor $method,
MethodReflection $methodReflection
Expand All @@ -62,36 +64,36 @@ private function returnTypeViolations(
return [];
}

return [sprintf(
return [RuleErrorBuilder::message(sprintf(
'Method %s::%s() cannot have %s as its return type - floats are not allowed.',
$methodReflection->getDeclaringClass()->getDisplayName(),
$methodReflection->getName(),
$method->getReturnType()->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}

/** @return string[]|null[] */
/** @return RuleError[]|null[] */
private function violationsForParameters(
ParametersAcceptor $function,
MethodReflection $methodReflection
) : array {
$parameters = $function->getParameters();

return array_map(
static function (ParameterReflection $parameter, int $index) use ($methodReflection) : ?string {
static function (ParameterReflection $parameter, int $index) use ($methodReflection) : ?RuleError {
if (! FloatTypeHelper::isFloat($parameter->getType())) {
return null;
}

return sprintf(
return RuleErrorBuilder::message(sprintf(
'Parameter #%d $%s of method %s::%s() cannot have %s as its type - floats are not allowed.',
$index + 1,
$parameter->getName(),
$methodReflection->getDeclaringClass()->getDisplayName(),
$methodReflection->getName(),
$parameter->getType()->describe(VerbosityLevel::typeOnly())
);
))->build();
},
array_values($parameters),
array_keys($parameters)
Expand Down
15 changes: 8 additions & 7 deletions src/DisallowFloatPropertyTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\VerbosityLevel;
use function assert;
use function sprintf;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\PropertyProperty>
*/
final class DisallowFloatPropertyTypeRule implements Rule
{
public function getNodeType() : string
Expand All @@ -21,31 +24,29 @@ public function getNodeType() : string
}

/**
* @return string[]
* {@inheritDoc}
*/
public function processNode(Node $node, Scope $scope) : array
{
assert($node instanceof PropertyProperty);

if (! $scope->isInClass()) {
throw new ShouldNotHappenException();
}

$classReflection = $scope->getClassReflection();
$propertyName = $node->name->toString();
$property = $classReflection->getNativeProperty($node->name->toString());
$propertyType = $property->getType();
$propertyType = $property->getReadableType();
if (! FloatTypeHelper::isFloat($propertyType)) {
return [];
}

return [
sprintf(
RuleErrorBuilder::message(sprintf(
'Property %s::$%s cannot have %s as its type - floats are not allowed.',
$property->getDeclaringClass()->getDisplayName(),
$propertyName,
$propertyType->describe(VerbosityLevel::typeOnly())
),
))->build(),
];
}
}
3 changes: 3 additions & 0 deletions tests/src/DisallowFloatAssignedToVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use PHPStan\Testing\RuleTestCase;
use Roave\PHPStan\Rules\Floats\DisallowFloatAssignedToVariableRule;

/**
* @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatAssignedToVariableRule>
*/
final class DisallowFloatAssignedToVariableRuleTest extends RuleTestCase
{
protected function getRule() : Rule
Expand Down
3 changes: 3 additions & 0 deletions tests/src/DisallowFloatEverywhereRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use PHPStan\Testing\RuleTestCase;
use Roave\PHPStan\Rules\Floats\DisallowFloatEverywhereRule;

/**
* @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatEverywhereRule>
*/
class DisallowFloatEverywhereRuleTest extends RuleTestCase
{
protected function getRule() : Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use PHPStan\Testing\RuleTestCase;
use Roave\PHPStan\Rules\Floats\DisallowFloatInFunctionSignatureRule;

final class DisallowFloatinFunctionSignatureRuleTest extends RuleTestCase
/**
* @extends \PHPStan\Testing\RuleTestCase<\Roave\PHPStan\Rules\Floats\DisallowFloatInFunctionSignatureRule>
*/
final class DisallowFloatInFunctionSignatureRuleTest extends RuleTestCase
{
protected function getRule() : Rule
{
Expand Down
Loading

0 comments on commit 00cd927

Please sign in to comment.