Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@psalm-check-type
and @psalm-check-type-exact
.
I initially added these as part of my TryAnalyzer rewrite to allow testing complicated `finally` types like this: ``` $foo = 1; try { $foo = 2; } catch (Exception $_) { $foo = 3; } finally { $bar = $foo; /** @psalm-check-type-exact $bar = 1|2|3 */; } /** @psalm-check-type-exact $bar = 2|3 */; ``` Using the `'assertions'` in tests doesn't work since the type is different inside the `finally`. I decided to extract the new annotation from the rest of my changes and do a separate pull request since I think others may find it useful, and it should be much easier to review than the entire TryAnalyzer rewrite.
- Loading branch information
1 parent
1ee7648
commit d09e420
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', | ||
]; | ||
} | ||
} |