2016-12-24 00:52:34 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Psalm\Checker\FileChecker;
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class BinaryOperationTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/** @var \PhpParser\Parser */
|
|
|
|
protected static $parser;
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
/** @var \Psalm\Checker\ProjectChecker */
|
|
|
|
protected $project_checker;
|
|
|
|
|
2016-12-24 00:52:34 +01:00
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
self::$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
FileChecker::clearCache();
|
2017-01-02 21:31:18 +01:00
|
|
|
$this->project_checker = new \Psalm\Checker\ProjectChecker();
|
2016-12-24 00:52:34 +01:00
|
|
|
$config = new TestConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRegularAddition()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
echo 5 + 4;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidOperand
|
|
|
|
*/
|
|
|
|
public function testBadAddition()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = "b" + 5;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDifferingNumericTypesAdditionInWeakMode()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
echo 5 + 4.1;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidOperand
|
|
|
|
*/
|
|
|
|
public function testDifferingNumericTypesAdditionInStrictMode()
|
|
|
|
{
|
|
|
|
Config::getInstance()->strict_binary_operands = true;
|
|
|
|
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
echo 5 + 4.1;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNumericAddition()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
$a = "5";
|
|
|
|
|
|
|
|
if (is_numeric($a)) {
|
|
|
|
echo $a + 4;
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConcatenation()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
echo "Hey " + "Jude,";
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConcatenationWithNumberInWeakMode()
|
|
|
|
{
|
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
echo "hi" . 5;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Psalm\Exception\CodeException
|
|
|
|
* @expectedExceptionMessage InvalidOperand
|
|
|
|
*/
|
|
|
|
public function testConcatenationWithNumberInStrictMode()
|
|
|
|
{
|
|
|
|
Config::getInstance()->strict_binary_operands = true;
|
2017-01-02 21:31:18 +01:00
|
|
|
|
2016-12-24 00:52:34 +01:00
|
|
|
$stmts = self::$parser->parse('<?php
|
|
|
|
echo "hi" . 5;
|
|
|
|
');
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
|
2016-12-24 00:52:34 +01:00
|
|
|
$context = new Context('somefile.php');
|
2017-01-02 21:31:18 +01:00
|
|
|
$file_checker->visitAndCheckMethods($context);
|
2016-12-24 00:52:34 +01:00
|
|
|
}
|
|
|
|
}
|