-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9876dee
commit 69c03bb
Showing
18 changed files
with
245 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension; | ||
|
||
class ThrowsAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testMethodThrowsAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/Throws/MethodThrowsAttribute.php'); | ||
$expectedErrors = [ | ||
'Method test\PhpStaticAnalysis\PHPStanExtension\data\Throws\MethodThrowsAttribute::countNoErrorName() has Exception in PHPDoc @throws tag but it\'s not thrown.' => 72, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
|
||
public function testFunctionThrowsAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/Throws/FunctionThrowsAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidMethodThrowsAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/Throws/InvalidMethodThrowsAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'PHPDoc tag @throws has invalid value (): Unexpected token "\n ", expected type at offset 14' => 10, | ||
'Parameter #1 ...$exceptions of attribute class PhpStaticAnalysis\Attributes\Throws constructor expects string, int given.' => 10, | ||
'Method test\PhpStaticAnalysis\PHPStanExtension\data\Throws\InvalidMethodThrowsAttribute::getOtherNameLength() has string in PHPDoc @throws tag but it\'s not thrown.' => 16, | ||
'PHPDoc tag @throws with type string is not subtype of Throwable' => 16, | ||
'Attribute class PhpStaticAnalysis\Attributes\Throws does not have the property target.' => 22, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return array_merge( | ||
parent::getAdditionalConfigFiles(), | ||
[ | ||
__DIR__ . '/conf/throws.neon', | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
parameters: | ||
exceptions: | ||
check: | ||
tooWideThrowType: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Throws; | ||
|
||
use Exception; | ||
use PhpStaticAnalysis\Attributes\Throws; | ||
|
||
#[Throws(Exception::class)] | ||
function countName(string $name): int | ||
{ | ||
if ($name == '') { | ||
throw new Exception('Empty string!'); | ||
} | ||
return strlen($name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Throws; | ||
|
||
use Exception; | ||
use PhpStaticAnalysis\Attributes\Throws; | ||
|
||
class InvalidMethodThrowsAttribute | ||
{ | ||
#[Throws(0)] | ||
public function getNameLength(string $name): int | ||
{ | ||
return strlen($name); | ||
} | ||
|
||
#[Throws('string')] | ||
public function getOtherNameLength(string $name): int | ||
{ | ||
return strlen($name); | ||
} | ||
|
||
#[Throws(Exception::class)] | ||
public string $property; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Throws; | ||
|
||
use Error; | ||
use Exception; | ||
use PhpStaticAnalysis\Attributes\Throws; | ||
|
||
class MethodThrowsAttribute | ||
{ | ||
#[Throws(Exception::class)] // returns the number of names | ||
public function countName(string $name): int | ||
{ | ||
if ($name == '') { | ||
throw new Exception('Empty string!'); | ||
} | ||
return strlen($name); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
#[Throws(Exception::class)] | ||
public function countMoreName(string $name): int | ||
{ | ||
if ($name == '') { | ||
throw new Exception('Empty string!'); | ||
} | ||
return strlen($name); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
#[Throws(Exception::class)] | ||
public function countEvenMoreName(string $name): int | ||
{ | ||
if ($name == '') { | ||
throw new Exception('Empty string!'); | ||
} | ||
return strlen($name); | ||
} | ||
|
||
#[Throws( | ||
Exception::class, | ||
Error::class | ||
)] | ||
public function countTwoNames(string $name1, string $name2): int | ||
{ | ||
if ($name1 == '') { | ||
throw new Exception('Empty string!'); | ||
} | ||
if ($name2 == '') { | ||
throw new Error('Empty string!'); | ||
} | ||
return strlen($name1 . $name2); | ||
} | ||
|
||
#[Throws(Exception::class)] | ||
#[Throws(Error::class)] | ||
public function countOtherTwoNames(string $name1, string $name2): int | ||
{ | ||
if ($name1 == '') { | ||
throw new Exception('Empty string!'); | ||
} | ||
if ($name2 == '') { | ||
throw new Error('Empty string!'); | ||
} | ||
return strlen($name1 . $name2); | ||
} | ||
|
||
#[Throws(Exception::class)] | ||
public function countNoErrorName(string $name): int | ||
{ | ||
return strlen($name); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function countNameExtra(string $name): int | ||
{ | ||
if ($name == '') { | ||
throw new Exception('Empty string!'); | ||
} | ||
return strlen($name); | ||
} | ||
} |
Oops, something went wrong.