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

148 lines
3.6 KiB
PHP
Raw Normal View History

<?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;
use Psalm\Context;
class Php55Test extends PHPUnit_Framework_TestCase
{
/** @var \PhpParser\Parser */
2016-11-02 07:29:00 +01:00
protected static $parser;
/** @var \Psalm\Checker\ProjectChecker */
protected $project_checker;
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public static function setUpBeforeClass()
{
2016-11-02 07:29:00 +01:00
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
2016-12-14 18:28:38 +01:00
$config = new TestConfig();
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function setUp()
{
2016-11-02 07:29:00 +01:00
FileChecker::clearCache();
$this->project_checker = new \Psalm\Checker\ProjectChecker();
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testGenerator()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
/**
* @param int $start
* @param int $limit
* @param int $step
2016-10-20 20:26:03 +02:00
* @return Generator<int>
*/
function xrange($start, $limit, $step = 1) {
for ($i = $start; $i <= $limit; $i += $step) {
yield $i;
}
}
2016-10-20 20:26:03 +02:00
$a = null;
/*
* Note that an array is never created or returned,
* which saves memory.
*/
foreach (xrange(1, 9, 2) as $number) {
2016-10-20 20:26:03 +02:00
$a = $number;
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
2016-10-20 20:26:03 +02:00
$this->assertEquals('null|int', (string) $context->vars_in_scope['$a']);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testFinally()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
try {
}
catch (\Exception $e) {
}
finally {
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testForeachList()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list($a, $b)) {
echo "A: $a; B: $b\n";
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testArrayStringDereferencing()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
$a = [1, 2, 3][0];
$b = "PHP"[0];
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('int', (string) $context->vars_in_scope['$a']);
$this->assertEquals('string', (string) $context->vars_in_scope['$b']);
}
2017-01-13 20:07:23 +01:00
/**
* @return void
*/
public function testClassString()
{
2016-11-02 07:29:00 +01:00
$stmts = self::$parser->parse('<?php
class ClassName {}
$a = ClassName::class;
?>
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context('somefile.php');
$file_checker->visitAndAnalyzeMethods($context);
$this->assertEquals('string', (string) $context->vars_in_scope['$a']);
}
}