Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7686 from AndrolGenhald/feature/type-check-annota…
…tions Add `@psalm-check-type` and `@psalm-check-type-exact`.
- Loading branch information
Showing
8 changed files
with
203 additions
and
1 deletion.
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,19 @@ | ||
# CheckType | ||
|
||
Checks if a variable matches a specific type. | ||
Similar to [Trace](./Trace.md), but only shows if the type does not match the expected type. | ||
|
||
```php | ||
<?php | ||
|
||
/** @psalm-check-type $x = 1 */ | ||
$x = 2; // Checked variable $x = 1 does not match $x = 2 | ||
``` | ||
|
||
|
||
```php | ||
<?php | ||
|
||
/** @psalm-check-type-exact $x = int */ | ||
$x = 2; // Checked variable $x = int does not match $x = 2 | ||
``` |
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,9 @@ | ||
<?php | ||
|
||
namespace Psalm\Issue; | ||
|
||
final class CheckType extends CodeIssue | ||
{ | ||
public const ERROR_LEVEL = 8; | ||
public const SHORTCODE = 311; | ||
} |
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,74 @@ | ||
<?php | ||
|
||
namespace Psalm\Tests; | ||
|
||
use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait; | ||
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait; | ||
|
||
class CheckTypeTest extends TestCase | ||
{ | ||
use InvalidCodeAnalysisTestTrait; | ||
use ValidCodeAnalysisTestTrait; | ||
|
||
/** | ||
* @return iterable<string,array{code:string,assertions?:array<string,string>,ignored_issues?:list<string>,php_version?:string}> | ||
*/ | ||
public function providerValidCodeParse(): iterable | ||
{ | ||
yield 'allowSubtype' => [ | ||
'code' => '<?php | ||
/** @psalm-check-type $foo = int */ | ||
$foo = 1; | ||
', | ||
]; | ||
} | ||
|
||
/** | ||
* @return iterable<string,array{code:string,error_message:string,ignored_issues?:list<string>,php_version?:string}> | ||
*/ | ||
public function providerInvalidCodeParse(): iterable | ||
{ | ||
yield 'checkType' => [ | ||
'code' => '<?php | ||
$foo = 1; | ||
/** @psalm-check-type $foo = 2 */; | ||
', | ||
'error_message' => 'CheckType', | ||
]; | ||
yield 'checkTypeExact' => [ | ||
'code' => '<?php | ||
/** @psalm-check-type-exact $foo = int */ | ||
$foo = 1; | ||
', | ||
'error_message' => 'CheckType', | ||
]; | ||
yield 'checkMultipleTypesFirstCorrect' => [ | ||
'code' => '<?php | ||
$foo = 1; | ||
$bar = 2; | ||
/** | ||
* @psalm-check-type $foo = 1 | ||
* @psalm-check-type $bar = 3 | ||
*/; | ||
', | ||
'error_message' => 'CheckType', | ||
]; | ||
yield 'possiblyUnset' => [ | ||
'code' => '<?php | ||
try { | ||
$foo = 1; | ||
} catch (Exception $_) { | ||
} | ||
/** @psalm-check-type $foo = 1 */; | ||
', | ||
'error_message' => 'Checked variable $foo = 1 does not match $foo? = 1', | ||
]; | ||
yield 'notPossiblyUnset' => [ | ||
'code' => '<?php | ||
$foo = 1; | ||
/** @psalm-check-type $foo? = 1 */; | ||
', | ||
'error_message' => 'Checked variable $foo? = 1 does not match $foo = 1', | ||
]; | ||
} | ||
} |