1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Improve assertions support a bit more

This commit is contained in:
Matthew Brown 2019-12-30 10:48:50 -05:00
parent 19faa31865
commit 366e2d30a3
3 changed files with 39 additions and 1 deletions

View File

@ -336,7 +336,11 @@ class AssertionFinder
if ($conditional instanceof PhpParser\Node\Expr\MethodCall
|| $conditional instanceof PhpParser\Node\Expr\StaticCall
) {
$if_types += self::processCustomAssertion($conditional, $this_class_name, $source, false);
$custom_assertions = self::processCustomAssertion($conditional, $this_class_name, $source, false);
if ($custom_assertions) {
return $custom_assertions;
}
return $if_types;
}

View File

@ -1340,6 +1340,12 @@ class ReflectionClass implements Reflector {
* @return T
*/
public function newInstanceWithoutConstructor(): object;
/**
* @return ?array<string>
* @psalm-ignore-nullable-return
*/
public function getTraitNames(): array {}
}
/**

View File

@ -794,6 +794,34 @@ class AssertAnnotationTest extends TestCase
}
}'
],
'assertOnThisMethod' => [
'<?php
/** @psalm-immutable */
class A {
private ?array $arr = null;
public function __construct(?array $arr) {
$this->arr = $arr;
}
/** @psalm-assert-if-true !null $this->getarray() */
public function hasArray() : bool {
return $this->arr !== null;
}
public function getArray() : ?array {
return $this->arr;
}
}
function foo(A $a) : void {
if (!$a->hasArray()) {
return;
}
echo count($a->getArray());
}'
],
];
}