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

251 lines
7.7 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests;
use const DIRECTORY_SEPARATOR;
class PureAnnotationTest extends TestCase
{
use Traits\InvalidCodeAnalysisTestTrait;
use Traits\ValidCodeAnalysisTestTrait;
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
*/
public function providerValidCodeParse()
{
return [
'simplePureFunction' => [
'<?php
namespace Bar;
class A {
public int $a = 5;
}
/** @psalm-pure */
function filterOdd(int $i, A $a) : ?int {
if ($i % 2 === 0 || $a->a === 2) {
return $i;
}
$a = new A();
return null;
}',
],
'pureFunctionCallingBuiltinFunctions' => [
'<?php
namespace Bar;
/** @psalm-pure */
function lower(string $s) : string {
return substr(strtolower($s), 0, 10);
}',
],
'pureWithStrReplace' => [
'<?php
/** @psalm-pure */
function highlight(string $needle, string $output) : string {
$needle = preg_quote($needle, \'#\');
$needles = str_replace([\'"\', \' \'], [\'\', \'|\'], $needle);
$output = preg_replace("#({$needles})#im", "<mark>$1</mark>", $output);
return $output;
}'
],
'implicitAnnotations' => [
'<?php
abstract class Foo {
private array $options;
private array $defaultOptions;
function __construct(array $options) {
$this->setOptions($options);
$this->setDefaultOptions($this->getOptions());
}
function getOptions(): array {
return $this->options;
}
function setOptions(array $options): void {
$this->options = $options;
}
function setDefaultOptions(array $defaultOptions): void {
$this->defaultOptions = $defaultOptions;
}
}',
],
'canCreateObjectWithNoExternalMutations' => [
'<?php
/** @psalm-external-mutation-free */
class Counter {
private int $count = 0;
public function __construct(int $count) {
$this->count = $count;
}
public function increment() : void {
$this->count++;
}
}
/** @psalm-pure */
function makesACounter(int $i) : Counter {
$c = new Counter($i);
$c->increment();
return $c;
}',
],
2019-08-31 16:07:23 +02:00
'canCreateImmutableObject' => [
'<?php
2019-09-01 00:43:39 +02:00
/** @psalm-immutable */
2019-08-31 16:07:23 +02:00
class A {
private string $s;
public function __construct(string $s) {
$this->s = $s;
}
public function getShort() : string {
return substr($this->s, 0, 5);
}
}
/** @psalm-pure */
function makeA(string $s) : A {
$a = new A($s);
if ($a->getShort() === "bar") {
return new A("foo");
}
return $a;
}'
],
];
}
/**
* @return iterable<string,array{string,error_message:string,2?:string[],3?:bool,4?:string}>
*/
public function providerInvalidCodeParse()
{
return [
'impurePropertyAssignment' => [
'<?php
namespace Bar;
class A {
public int $a = 5;
}
/** @psalm-pure */
function filterOdd(int $i, A $a) : ?int {
$a->a++;
if ($i % 2 === 0 || $a->a === 2) {
return $i;
}
return null;
}',
'error_message' => 'ImpurePropertyAssignment',
],
'impureMethodCall' => [
'<?php
namespace Bar;
class A {
public int $a = 5;
public function foo() : void {
$this->a++;
}
}
/** @psalm-pure */
function filterOdd(int $i, A $a) : ?int {
$a->foo();
if ($i % 2 === 0 || $a->a === 2) {
return $i;
}
return null;
}',
'error_message' => 'ImpureMethodCall',
],
'impureFunctionCall' => [
'<?php
namespace Bar;
function impure() : ?string {
/** @var int */
static $i = 0;
++$i;
return $i % 2 ? "hello" : null;
}
/** @psalm-pure */
function filterOdd(array $a) : void {
impure();
}',
'error_message' => 'ImpureFunctionCall',
],
'impureConstructorCall' => [
'<?php
namespace Bar;
class A {
public int $a = 5;
}
class B {
public function __construct(A $a) {
$a->a++;
}
}
/** @psalm-pure */
function filterOdd(int $i, A $a) : ?int {
$b = new B($a);
if ($i % 2 === 0 || $a->a === 2) {
return $i;
}
return null;
}',
'error_message' => 'ImpureMethodCall',
],
'canCreateObjectWithNoExternalMutations' => [
'<?php
class Counter {
private int $count = 0;
public function __construct(int $count) {
$this->count = $count;
}
public function increment() : void {
$this->count += rand(0, 5);
}
}
/** @psalm-pure */
function makesACounter(int $i) : Counter {
$c = new Counter($i);
$c->increment();
return $c;
}',
'error_message' => 'ImpureMethodCall',
],
];
}
}