generated from psalm/psalm-plugin-skeleton
-
-
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
377c5f1
commit b62b26c
Showing
15 changed files
with
520 additions
and
30 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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace test\PhpStaticAnalysis\PsalmPlugin; | ||
|
||
class AssertAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testFunctionAssertAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/Assert/FunctionAssertAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testMethodAssertAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/Assert/MethodAssertAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidMethodAssertAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/Assert/InvalidMethodAssertAttribute.php'); | ||
$this->checkExpectedErrors($errors,[ | ||
'Argument 1 of PhpStaticAnalysis\Attributes\Assert::__construct expects string, but 0 provided' => 9, | ||
'Attribute Assert cannot be used on a property' => 14, | ||
]); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace test\PhpStaticAnalysis\PsalmPlugin; | ||
|
||
class AssertIfFalseAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testFunctionAssertIfFalseAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/AssertIfFalse/FunctionAssertIfFalseAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testMethodAssertIfFalseAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/AssertIfFalse/MethodAssertIfFalseAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidMethodAssertIfFalseAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/AssertIfFalse/InvalidMethodAssertIfFalseAttribute.php'); | ||
$this->checkExpectedErrors($errors,[ | ||
'Argument 1 of PhpStaticAnalysis\Attributes\AssertIfFalse::__construct expects string, but 0 provided' => 9, | ||
'Attribute AssertIfFalse cannot be used on a property' => 15, | ||
]); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace test\PhpStaticAnalysis\PsalmPlugin; | ||
|
||
class AssertIfTrueAttributeTest extends BaseAttributeTestCase | ||
{ | ||
public function testFunctionAssertIfTrueAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/AssertIfTrue/FunctionAssertIfTrueAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testMethodAssertIfTrueAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/AssertIfTrue/MethodAssertIfTrueAttribute.php'); | ||
$this->assertCount(0, $errors); | ||
} | ||
|
||
public function testInvalidMethodAssertIfTrueAttribute(): void | ||
{ | ||
$errors = $this->analyzeTestFile('/data/AssertIfTrue/InvalidMethodAssertIfTrueAttribute.php'); | ||
$this->checkExpectedErrors($errors,[ | ||
'Argument 1 of PhpStaticAnalysis\Attributes\AssertIfTrue::__construct expects string, but 0 provided' => 9, | ||
'Attribute AssertIfTrue cannot be used on a property' => 15, | ||
]); | ||
} | ||
} |
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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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\PsalmPlugin\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.