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

Improve null coalesce test and improve linting

This commit is contained in:
Matt Brown 2017-06-20 14:38:13 -04:00
parent e1d5eab7a2
commit 10b511284a
3 changed files with 35 additions and 35 deletions

View File

@ -17,14 +17,14 @@ class Php70Test extends TestCase
function indexof(string $haystack, string $needle) : int
{
$pos = strpos($haystack, $needle);
if ($pos === false) {
return -1;
}
return $pos;
}
$a = indexof("arr", "a");',
'assertions' => [
['int' => '$a'],
@ -36,15 +36,15 @@ class Php70Test extends TestCase
public static function indexof(string $haystack, string $needle) : int
{
$pos = strpos($haystack, $needle);
if ($pos === false) {
return -1;
}
return $pos;
}
}
$a = Foo::indexof("arr", "a");',
'assertions' => [
['int' => '$a'],
@ -52,11 +52,11 @@ class Php70Test extends TestCase
],
'nullCoalesce' => [
'<?php
$a = $_GET["bar"] ?? "nobody";',
$arr = ["hello", "goodbye"];
$a = $arr[rand(0, 10)] ?? null;',
'assertions' => [
['mixed' => '$a'],
['string|null' => '$a'],
],
'error_levels' => ['MixedAssignment'],
],
'spaceship' => [
'<?php
@ -72,7 +72,7 @@ class Php70Test extends TestCase
"cat",
"bird"
]);
$a = ANIMALS[1];',
'assertions' => [
['string' => '$a'],
@ -84,17 +84,17 @@ class Php70Test extends TestCase
/** @return void */
public function log(string $msg);
}
class Application {
/** @var Logger|null */
private $logger;
/** @return void */
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setLogger(new class implements Logger {
public function log(string $msg) {
@ -109,11 +109,11 @@ class Php70Test extends TestCase
return 42;
}
};
function g(int $i) : int {
return $i;
}
$x = g($class->f());',
],
'generatorWithReturn' => [
@ -126,7 +126,7 @@ class Php70Test extends TestCase
if ($i === 1) {
return "bash";
}
yield 1;
}',
],
@ -144,7 +144,7 @@ class Php70Test extends TestCase
yield from seven_eight();
return yield from nine_ten();
}
/**
* @return Generator<int,int>
*/
@ -152,14 +152,14 @@ class Php70Test extends TestCase
yield 7;
yield from eight();
}
/**
* @return Generator<int,int>
*/
function eight() : Generator {
yield 8;
}
/**
* @return Generator<int,int>
* @psalm-generator-return int
@ -168,7 +168,7 @@ class Php70Test extends TestCase
yield 9;
return 10;
}
$gen = count_to_ten();
foreach ($gen as $num) {
echo "$num ";
@ -184,20 +184,20 @@ class Php70Test extends TestCase
'<?php
namespace Name\Space {
class A {
}
class B {
}
}
namespace Noom\Spice {
use Name\Space\{
A,
B
};
new A();
new B();
}',

View File

@ -41,13 +41,13 @@ class ReferenceConstraintTest extends TestCase
class A {
/** @var int */
private $foo;
public function __construct(int &$foo) {
$this->foo = &$foo;
$foo = "hello";
}
}
$bar = 5;
$a = new A($bar); // $bar is constrained to an int
$bar = null; // ReferenceConstraintViolation issue emitted',
@ -58,12 +58,12 @@ class ReferenceConstraintTest extends TestCase
class A {
/** @var int */
private $foo;
public function __construct(int &$foo) {
$this->foo = &$foo;
}
}
$bar = 5;
$a = new A($bar);
$bar = null;',
@ -74,21 +74,21 @@ class ReferenceConstraintTest extends TestCase
class A {
/** @var int */
private $foo;
public function __construct(int &$foo) {
$this->foo = &$foo;
}
}
class B {
/** @var string */
private $bar;
public function __construct(string &$bar) {
$this->bar = &$bar;
}
}
if (rand(0, 1)) {
$v = 5;
$c = (new A($v)); // $v is constrained to an int
@ -96,7 +96,7 @@ class ReferenceConstraintTest extends TestCase
$v = "hello";
$c = (new B($v)); // $v is constrained to a string
}
$v = 8;',
'error_message' => 'ConflictingReferenceConstraint',
],

View File

@ -109,7 +109,7 @@ class ToStringTest extends TestCase
takesString($i);
}',
'error_message' => 'ImplicitToStringCast',
]
],
];
}
}