1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00
psalm/tests/Php70Test.php

255 lines
6.5 KiB
PHP
Raw Normal View History

2016-10-19 04:02:38 +02:00
<?php
namespace Psalm\Tests;
use PhpParser\ParserFactory;
use PHPUnit_Framework_TestCase;
2016-11-02 07:29:00 +01:00
use Psalm\Checker\FileChecker;
use Psalm\Config;
2016-10-19 04:02:38 +02:00
use Psalm\Context;
class Php70Test extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
2016-11-02 07:29:00 +01:00
protected static $parser;
2016-10-19 04:02:38 +02:00
public static function setUpBeforeClass()
{
2016-11-02 07:29:00 +01:00
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-10-19 04:02:38 +02:00
}
public function setUp()
{
$config = new TestConfig();
2016-11-02 07:29:00 +01:00
FileChecker::clearCache();
2016-10-19 04:02:38 +02:00
}
public function testFunctionTypeHints()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-19 04:02:38 +02:00
function indexof(string $haystack, string $needle) : int
{
$pos = strpos($haystack, $needle);
if ($pos === false) {
return -1;
}
return $pos;
}
$a = indexof("arr", "a");
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-19 04:02:38 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
public function testMethodTypeHints()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-19 04:02:38 +02:00
class Foo {
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");
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-19 04:02:38 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
2016-10-20 06:47:10 +02:00
public function testNullCoalesce()
{
2016-12-30 02:07:42 +01:00
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-20 06:47:10 +02:00
$a = $_GET["bar"] ?? "nobody";
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-20 06:47:10 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('mixed', (string) $context->vars_in_scope['$a']);
}
public function testSpaceship()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-20 06:47:10 +02:00
$a = 1 <=> 1;
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-20 06:47:10 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
}
public function testDefineArray()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-20 06:47:10 +02:00
define("ANIMALS", [
"dog",
"cat",
"bird"
]);
$a = ANIMALS[1];
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-20 06:47:10 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
}
public function testAnonymousClass()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-20 06:47:10 +02:00
interface Logger {
2016-11-11 23:13:24 +01:00
/** @return void */
2016-10-20 06:47:10 +02:00
public function log(string $msg);
}
class Application {
2016-11-01 19:32:19 +01:00
/** @var Logger|null */
2016-10-20 06:47:10 +02:00
private $logger;
2016-11-11 23:13:24 +01:00
/** @return void */
2016-10-20 06:47:10 +02:00
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
}
$app = new Application;
$app->setLogger(new class implements Logger {
public function log(string $msg) {
echo $msg;
}
});
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-20 06:47:10 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-10-20 20:26:03 +02:00
public function testGeneratorWithReturn()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-20 20:26:03 +02:00
/**
2016-10-21 00:05:28 +02:00
* @return Generator<int,int>
* @psalm-generator-return string
2016-10-20 20:26:03 +02:00
*/
function foo(int $i) : Generator {
if ($i === 1) {
return "bash";
}
yield 1;
}
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-20 20:26:03 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
2016-10-20 06:47:10 +02:00
}
public function testGeneratorDelegation()
{
2016-12-30 02:07:42 +01:00
Config::getInstance()->setCustomErrorLevel('MixedAssignment', Config::REPORT_SUPPRESS);
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
2016-10-20 17:31:19 +02:00
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-21 00:05:28 +02:00
* @psalm-generator-return int
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function count_to_ten() : Generator {
2016-10-20 06:47:10 +02:00
yield 1;
yield 2;
2016-10-20 17:31:19 +02:00
yield from [3, 4];
yield from new ArrayIterator([5, 6]);
yield from seven_eight();
return yield from nine_ten();
2016-10-20 06:47:10 +02:00
}
2016-10-20 17:31:19 +02:00
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function seven_eight() : Generator {
2016-10-20 17:31:19 +02:00
yield 7;
yield from eight();
2016-10-20 06:47:10 +02:00
}
2016-10-20 17:31:19 +02:00
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function eight() : Generator {
2016-10-20 17:31:19 +02:00
yield 8;
}
/**
2016-10-20 20:26:03 +02:00
* @return Generator<int,int>
2016-10-21 00:05:28 +02:00
* @psalm-generator-return int
2016-10-20 17:31:19 +02:00
*/
2016-10-20 20:26:03 +02:00
function nine_ten() : Generator {
2016-10-20 17:31:19 +02:00
yield 9;
return 10;
2016-10-20 06:47:10 +02:00
}
2016-10-20 17:31:19 +02:00
$gen = count_to_ten();
foreach ($gen as $num) {
echo "$num ";
}
$gen2 = $gen->getReturn();
2016-10-20 06:47:10 +02:00
');
2016-11-02 07:29:00 +01:00
$file_checker = new FileChecker('somefile.php', $stmts);
2016-10-20 06:47:10 +02:00
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
2016-11-13 17:24:25 +01:00
$this->assertEquals('Generator<int, int>', (string) $context->vars_in_scope['$gen']);
2016-10-20 20:26:03 +02:00
$this->assertEquals('mixed', (string) $context->vars_in_scope['$gen2']);
2016-10-20 06:47:10 +02:00
}
2016-11-21 04:40:19 +01:00
public function testMultipleUse()
{
$stmts = self::$parser->parse('<?php
namespace Name\Space {
class A {
}
class B {
}
}
namespace Noom\Spice {
use Name\Space\{
A,
B
};
new A();
new B();
}
');
$file_checker = new FileChecker('somefile.php', $stmts);
$context = new Context('somefile.php');
$file_checker->check(true, true, $context);
}
2016-10-19 04:02:38 +02:00
}