-
-
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.
Add Assert, AssertIfTrue and AssertIfFalse attributes
- Loading branch information
1 parent
44f151d
commit 3aaf8e1
Showing
15 changed files
with
497 additions
and
4 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,30 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension; | ||
|
||
class AssertAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testMethodAssertAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/Assert/MethodAssertAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testFunctionAssertAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/Assert/FunctionAssertAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidMethodAssertAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/Assert/InvalidMethodAssertAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\Assert constructor expects string, int given.' => 9, | ||
'Attribute class PhpStaticAnalysis\Attributes\Assert does not have the property target.' => 14, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
|
||
use test\PhpStaticAnalysis\PHPStanExtension\BaseAttributeTestCase; | ||
|
||
class AssertIfFalseAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testMethodAssertIfFalseAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/AssertIfFalse/MethodAssertIfFalseAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testFunctionAssertIfFalseAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/AssertIfFalse/FunctionAssertIfFalseAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidMethodAssertIfFalseAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/AssertIfFalse/InvalidMethodAssertIfFalseAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\AssertIfFalse constructor expects string, int given.' => 9, | ||
'Attribute class PhpStaticAnalysis\Attributes\AssertIfFalse does not have the property target.' => 15, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
|
||
use test\PhpStaticAnalysis\PHPStanExtension\BaseAttributeTestCase; | ||
|
||
class AssertIfTrueAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testMethodAssertIfTrueAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/AssertIfTrue/MethodAssertIfTrueAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testFunctionAssertIfTrueAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/AssertIfTrue/FunctionAssertIfTrueAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidMethodAssertIfTrueAttribute(): void | ||
{ | ||
$errors = $this->analyse(__DIR__ . '/data/AssertIfTrue/InvalidMethodAssertIfTrueAttribute.php'); | ||
|
||
$expectedErrors = [ | ||
'Parameter #1 ...$params of attribute class PhpStaticAnalysis\Attributes\AssertIfTrue constructor expects string, int given.' => 9, | ||
'Attribute class PhpStaticAnalysis\Attributes\AssertIfTrue does not have the property target.' => 15, | ||
]; | ||
|
||
$this->checkExpectedErrors($errors, $expectedErrors); | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Assert; | ||
|
||
use Exception; | ||
use PhpStaticAnalysis\Attributes\Assert; | ||
|
||
#[Assert(name: 'string')] | ||
function checkString(mixed $name): void | ||
{ | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Assert; | ||
|
||
use PhpStaticAnalysis\Attributes\Assert; | ||
|
||
class InvalidMethodAssertAttribute | ||
{ | ||
#[Assert(0)] | ||
public function checkString(mixed $name): void | ||
{ | ||
} | ||
|
||
#[Assert(property: 'string')] | ||
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,114 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\Assert; | ||
|
||
use Exception; | ||
use PhpStaticAnalysis\Attributes\Assert; | ||
|
||
class MethodAssertAttribute | ||
{ | ||
#[Assert(name: 'string')] // checks name is string | ||
public function checkString(mixed $name): void | ||
{ | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
#[Assert(exception: Exception::class)] | ||
public function checkException(mixed $exception): void | ||
{ | ||
if (!$exception instanceof Exception) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
#[Assert('string $name')] | ||
public function checkOtherString(mixed $name): void | ||
{ | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
#[Assert(name: 'string')] | ||
public function checkAnotherString(mixed $name): void | ||
{ | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
/** | ||
* @assert int $name | ||
*/ | ||
#[Assert(name: 'string')] | ||
public function checkEvenMoreString(mixed $name): void | ||
{ | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
#[Assert( | ||
name1: 'string', | ||
name2: 'string' | ||
)] | ||
public function checkStrings(mixed $name1, mixed $name2): void | ||
{ | ||
if (!is_string($name1) || !is_string($name2)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
#[Assert(name1: 'string')] | ||
#[Assert(name2: 'string')] | ||
public function checkOtherStrings(mixed $name1, mixed $name2): void | ||
{ | ||
if (!is_string($name1) || !is_string($name2)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
/** | ||
* @assert string $name | ||
*/ | ||
public function checkMoreAndMoreString(mixed $name): void | ||
{ | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
public function checkStringInParam( | ||
#[Assert('string')] | ||
mixed $name | ||
): void { | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
public function checkStringInParamWithName( | ||
#[Assert(name: 'string')] | ||
mixed $name | ||
): void { | ||
if (!is_string($name)) { | ||
throw new Exception(); | ||
} | ||
} | ||
|
||
public function checkStringInTwoParams( | ||
#[Assert('string')] | ||
mixed $name1, | ||
#[Assert('string')] | ||
mixed $name2 | ||
): void { | ||
if (!is_string($name1) || !is_string($name2)) { | ||
throw new Exception(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/data/AssertIfFalse/FunctionAssertIfFalseAttribute.php
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,12 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\AssertIfFalse; | ||
|
||
use Exception; | ||
use PhpStaticAnalysis\Attributes\AssertIfFalse; | ||
|
||
#[AssertIfFalse(name: 'string')] | ||
function checkString(mixed $name): bool | ||
{ | ||
return !is_string($name); | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/data/AssertIfFalse/InvalidMethodAssertIfFalseAttribute.php
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,17 @@ | ||
<?php | ||
|
||
namespace test\PhpStaticAnalysis\PHPStanExtension\data\AssertIfFalse; | ||
|
||
use PhpStaticAnalysis\Attributes\AssertIfFalse; | ||
|
||
class InvalidMethodAssertIfFalseAttribute | ||
{ | ||
#[AssertIfFalse(0)] | ||
public function checkString(mixed $name): bool | ||
{ | ||
return false; | ||
} | ||
|
||
#[AssertIfFalse(property: 'string')] | ||
public string $property; | ||
} |
Oops, something went wrong.