Skip to content

Commit ded27fc

Browse files
committed
Fix Assert::stringNotEmpty
1 parent 387b030 commit ded27fc

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "~7.1",
10-
"phpstan/phpstan": "^0.12"
10+
"phpstan/phpstan": "^0.12.6"
1111
},
1212
"require-dev": {
1313
"consistence/coding-standard": "^3.7",

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace PHPStan\Type\WebMozartAssert;
44

55
use PhpParser\Node\Arg;
6+
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
7+
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
68
use PhpParser\Node\Expr\StaticCall;
9+
use PhpParser\Node\Scalar\String_;
710
use PHPStan\Analyser\Scope;
811
use PHPStan\Analyser\SpecifiedTypes;
912
use PHPStan\Analyser\TypeSpecifier;
@@ -181,9 +184,15 @@ private static function getExpressionResolvers(): array
181184
);
182185
},
183186
'stringNotEmpty' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
184-
return new \PhpParser\Node\Expr\FuncCall(
185-
new \PhpParser\Node\Name('is_string'),
186-
[$value]
187+
return new BooleanAnd(
188+
new \PhpParser\Node\Expr\FuncCall(
189+
new \PhpParser\Node\Name('is_string'),
190+
[$value]
191+
),
192+
new NotIdentical(
193+
$value->value,
194+
new String_('')
195+
)
187196
);
188197
},
189198
'float' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\WebMozartAssert;
4+
5+
use PHPStan\Rules\Comparison\ImpossibleCheckTypeHelper;
6+
use PHPStan\Rules\Comparison\ImpossibleCheckTypeStaticMethodCallRule;
7+
use PHPStan\Rules\Rule;
8+
9+
/**
10+
* @extends \PHPStan\Testing\RuleTestCase<ImpossibleCheckTypeStaticMethodCallRule>
11+
*/
12+
class ImpossibleCheckTypeMethodCallRuleTest extends \PHPStan\Testing\RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new ImpossibleCheckTypeStaticMethodCallRule(new ImpossibleCheckTypeHelper($this->createBroker(), $this->getTypeSpecifier(), []), true);
18+
}
19+
20+
/**
21+
* @return \PHPStan\Type\StaticMethodTypeSpecifyingExtension[]
22+
*/
23+
protected function getStaticMethodTypeSpecifyingExtensions(): array
24+
{
25+
return [
26+
new AssertTypeSpecifyingExtension(),
27+
];
28+
}
29+
30+
public function testExtension(): void
31+
{
32+
$this->analyse([__DIR__ . '/data/impossible-check.php'], [
33+
[
34+
'Call to static method Webmozart\Assert\Assert::stringNotEmpty() with \'\' will always evaluate to false.',
35+
13,
36+
],
37+
]);
38+
}
39+
40+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace WebmozartAssertImpossibleCheck;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo(string $s): void
11+
{
12+
Assert::stringNotEmpty($s);
13+
Assert::stringNotEmpty('');
14+
}
15+
16+
}

0 commit comments

Comments
 (0)