2019-03-29 00:53:22 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Tests;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Context;
|
|
|
|
|
|
|
|
class ThrowsInGlobalScopeTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 00:53:22 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtDocumentedThrowCall()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
|
2019-03-29 00:53:22 +01:00
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @throws RangeException
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function foo(int $x, int $y) : int {
|
|
|
|
if ($y === 0) {
|
|
|
|
throw new \RangeException("Cannot divide by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y < 0) {
|
|
|
|
throw new \InvalidArgumentException("This is also bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return intdiv($x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(0, 0);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 00:53:22 +01:00
|
|
|
*/
|
|
|
|
public function testCaughtDocumentedThrowCall()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_docblock = true;
|
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @throws RangeException
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function foo(int $x, int $y) : int {
|
|
|
|
if ($y === 0) {
|
|
|
|
throw new \RangeException("Cannot divide by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y < 0) {
|
|
|
|
throw new \InvalidArgumentException("This is also bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return intdiv($x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
foo(0, 0);
|
|
|
|
} catch (Exception $e) {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 00:53:22 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtUndocumentedThrowCall()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
function foo(int $x, int $y) : int {
|
|
|
|
if ($y === 0) {
|
|
|
|
throw new \RangeException("Cannot divide by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y < 0) {
|
|
|
|
throw new \InvalidArgumentException("This is also bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return intdiv($x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(0, 0);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 00:53:22 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtDocumentedThrowCallInNamespace()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
|
2019-03-29 00:53:22 +01:00
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace ns;
|
|
|
|
/**
|
2019-08-13 05:42:51 +02:00
|
|
|
* @throws \RangeException
|
|
|
|
* @throws \InvalidArgumentException
|
2019-03-29 00:53:22 +01:00
|
|
|
*/
|
|
|
|
function foo(int $x, int $y) : int {
|
|
|
|
if ($y === 0) {
|
|
|
|
throw new \RangeException("Cannot divide by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y < 0) {
|
|
|
|
throw new \InvalidArgumentException("This is also bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return intdiv($x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
foo(0, 0);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 00:53:22 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtThrow()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
|
|
|
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
|
|
|
|
|
2019-03-29 00:53:22 +01:00
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
throw new \Exception();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 00:53:22 +01:00
|
|
|
*/
|
|
|
|
public function testCaughtThrow()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
try {
|
|
|
|
throw new \Exception();
|
|
|
|
} catch (\Exception $e) {}'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
2019-03-29 01:01:26 +01:00
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 01:01:26 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtThrowWhenSuppressing()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
|
|
|
throw new \Exception();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 01:01:26 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtThrowInNamespaceWhenSuppressing()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace ns;
|
|
|
|
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
|
|
|
throw new \Exception();'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 01:01:26 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtDocumentedThrowCallWhenSuppressing()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @throws RangeException
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function foo(int $x, int $y) : int {
|
|
|
|
if ($y === 0) {
|
|
|
|
throw new \RangeException("Cannot divide by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y < 0) {
|
|
|
|
throw new \InvalidArgumentException("This is also bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return intdiv($x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
|
|
|
foo(0, 0);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-03-29 01:01:26 +01:00
|
|
|
*/
|
|
|
|
public function testUncaughtDocumentedThrowCallInNamespaceWhenSuppressing()
|
|
|
|
{
|
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
namespace ns;
|
|
|
|
/**
|
2019-08-13 05:42:51 +02:00
|
|
|
* @throws \RangeException
|
|
|
|
* @throws \InvalidArgumentException
|
2019-03-29 01:01:26 +01:00
|
|
|
*/
|
|
|
|
function foo(int $x, int $y) : int {
|
|
|
|
if ($y === 0) {
|
|
|
|
throw new \RangeException("Cannot divide by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y < 0) {
|
|
|
|
throw new \InvalidArgumentException("This is also bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return intdiv($x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
|
|
|
foo(0, 0);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
2019-04-03 01:42:23 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-17 00:36:36 +02:00
|
|
|
* @return void
|
2019-04-03 01:42:23 +02:00
|
|
|
*/
|
|
|
|
public function testUncaughtDocumentedThrowCallWhenSuppressingFirst()
|
|
|
|
{
|
2019-05-17 00:36:36 +02:00
|
|
|
$this->expectExceptionMessage('UncaughtThrowInGlobalScope');
|
|
|
|
$this->expectException(\Psalm\Exception\CodeException::class);
|
2019-04-03 01:42:23 +02:00
|
|
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
|
|
|
|
|
|
|
$this->addFile(
|
|
|
|
'somefile.php',
|
|
|
|
'<?php
|
|
|
|
/**
|
|
|
|
* @throws RangeException
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
function foo(int $x, int $y) : int {
|
|
|
|
if ($y === 0) {
|
|
|
|
throw new \RangeException("Cannot divide by zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y < 0) {
|
|
|
|
throw new \InvalidArgumentException("This is also bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
return intdiv($x, $y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @psalm-suppress UncaughtThrowInGlobalScope */
|
|
|
|
foo(0, 0);
|
|
|
|
|
|
|
|
foo(0, 0);'
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new Context();
|
|
|
|
|
|
|
|
$this->analyzeFile('somefile.php', $context);
|
|
|
|
}
|
2019-03-29 00:53:22 +01:00
|
|
|
}
|