mirror of
https://github.com/danog/psalm.git
synced 2024-12-11 16:59:45 +01:00
Inherit uncaught throws from namespace scope
This commit is contained in:
parent
3a11e1ff5b
commit
5a59963e9b
@ -89,6 +89,11 @@ class NamespaceAnalyzer extends SourceAnalyzer implements StatementsSource
|
|||||||
$context->defineGlobals();
|
$context->defineGlobals();
|
||||||
$context->collect_exceptions = $codebase->config->check_for_throws_in_global_scope;
|
$context->collect_exceptions = $codebase->config->check_for_throws_in_global_scope;
|
||||||
$statements_analyzer->analyze($leftover_stmts, $context);
|
$statements_analyzer->analyze($leftover_stmts, $context);
|
||||||
|
|
||||||
|
$file_context = $this->source->context;
|
||||||
|
if ($file_context) {
|
||||||
|
$file_context->possibly_thrown_exceptions += $context->possibly_thrown_exceptions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -492,4 +492,42 @@ class ThrowsAnnotationTest extends TestCase
|
|||||||
|
|
||||||
$this->analyzeFile('somefile.php', $context);
|
$this->analyzeFile('somefile.php', $context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Psalm\Exception\CodeException
|
||||||
|
* @expectedExceptionMessage UncaughtThrowInGlobalScope
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testUncaughtDocumentedThrowCallInNamespace()
|
||||||
|
{
|
||||||
|
Config::getInstance()->check_for_throws_in_global_scope = true;
|
||||||
|
|
||||||
|
$this->addFile(
|
||||||
|
'somefile.php',
|
||||||
|
'<?php
|
||||||
|
namespace ns;
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user