Skip to content
Permalink
Browse files

Inherit throws docblock from parent class by default (#1947)

  • Loading branch information...
smelesh authored and muglug committed Jul 14, 2019
1 parent 96429ff commit af875439e745d8e8ce723d51ee25c700d83da480
Showing with 116 additions and 8 deletions.
  1. +6 −8 src/Psalm/Internal/Codebase/Populator.php
  2. +110 −0 tests/ThrowsAnnotationTest.php
@@ -303,11 +303,10 @@ private function populateOverriddenMethods(
$declaring_method_storage->overridden_downstream = true;
$declaring_method_storage->overridden_somewhere = true;
if (!$method_storage->throws
&& $method_storage->inheritdoc
&& $declaring_method_storage->throws
if ($declaring_method_storage->throws
&& (!$method_storage->throws || $method_storage->inheritdoc)
) {
$method_storage->throws = $declaring_method_storage->throws;
$method_storage->throws += $declaring_method_storage->throws;
}
if (count($storage->overridden_method_ids[$method_name]) === 1
@@ -774,11 +773,10 @@ private function populateDataFromImplementedInterfaces(
if (isset($interface_storage->methods[$method_name])) {
$interface_method_storage = $interface_storage->methods[$method_name];
if (!$method_storage->throws
&& $method_storage->inheritdoc
&& $interface_method_storage->throws
if ($interface_method_storage->throws
&& (!$method_storage->throws || $method_storage->inheritdoc)
) {
$method_storage->throws = $interface_method_storage->throws;
$method_storage->throws += $interface_method_storage->throws;
}
if ($interface_method_storage->return_type
@@ -448,6 +448,113 @@ public function test(): void
* @return void
*/
public function testDocumentedThrowInInterfaceWithoutInheritDocblock()
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
public function test(): void
{
throw new \InvalidArgumentException();
}
}
'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testDocumentedThrowInSubclassWithExtendedInheritDocblock()
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
/**
* {@inheritdoc}
* @throws \OutOfBoundsException
*/
public function test(): void
{
throw new \OutOfBoundsException();
}
}
'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testDocumentedThrowInInterfaceWithExtendedInheritDocblock()
{
Config::getInstance()->check_for_throws_docblock = true;
$this->addFile(
'somefile.php',
'<?php
interface Foo
{
/**
* @throws \InvalidArgumentException
*/
public function test(): void;
}
class Bar implements Foo
{
/**
* {@inheritdoc}
* @throws \OutOfBoundsException
*/
public function test(): void
{
throw new \InvalidArgumentException();
}
}
'
);
$context = new Context();
$this->analyzeFile('somefile.php', $context);
}
/**
* @return void
*/
public function testDocumentedThrowInInterfaceWithOverriddenDocblock()
{
$this->expectExceptionMessage('MissingThrowsDocblock');
$this->expectException(\Psalm\Exception\CodeException::class);
@@ -466,6 +573,9 @@ public function test(): void;
class Bar implements Foo
{
/**
* @throws \OutOfBoundsException
*/
public function test(): void
{
throw new \InvalidArgumentException();

0 comments on commit af87543

Please sign in to comment.
You can’t perform that action at this time.