1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add new issue conditional on config flag

This commit is contained in:
Matt Brown 2018-03-06 12:19:50 -05:00
parent 357ad1aa82
commit b664c85642
8 changed files with 60 additions and 10 deletions

View File

@ -137,6 +137,7 @@
<xs:element name="InvalidScope" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidStaticInvocation" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidStaticVariable" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidStringClass" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidThrow" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidToString" type="IssueHandlerType" minOccurs="0" />
<xs:element name="LessSpecificReturnStatement" type="IssueHandlerType" minOccurs="0" />

View File

@ -340,6 +340,16 @@ class Foo {}
(new foo());
```
### InvalidStringClass
Emitted when you have `allowStringToStandInForClass="false"` in your config and youre passing a string instead of calling a class directly
```php
class Foo {}
$a = "Foo";
new $a();
```
### InvalidClone
Emitted when trying to clone a value that's not cloneable

View File

@ -12,8 +12,9 @@ use Psalm\Context;
use Psalm\Issue\AbstractInstantiation;
use Psalm\Issue\DeprecatedClass;
use Psalm\Issue\InterfaceInstantiation;
use Psalm\Issue\InvalidClass;
use Psalm\Issue\InvalidStringClass;
use Psalm\Issue\TooManyArguments;
use Psalm\Issue\UndefinedClass;
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic\TNamedObject;
@ -125,12 +126,26 @@ class NewChecker extends \Psalm\Checker\Statements\Expression\CallChecker
) {
continue;
}
} elseif ($lhs_type_part instanceof Type\Atomic\TMixed) {
if (IssueBuffer::accepts(
new InvalidStringClass(
'String cannot be used as a class',
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
continue;
}
if ($lhs_type_part instanceof Type\Atomic\TMixed) {
continue;
}
if (IssueBuffer::accepts(
new InvalidClass(
new UndefinedClass(
'Type ' . $lhs_type_part . ' cannot be called as a class',
new CodeLocation($statements_checker->getSource(), $stmt)
),

View File

@ -11,8 +11,9 @@ use Psalm\Config;
use Psalm\Context;
use Psalm\FileManipulation\FileManipulationBuffer;
use Psalm\Issue\DeprecatedClass;
use Psalm\Issue\InvalidClass;
use Psalm\Issue\InvalidStringClass;
use Psalm\Issue\ParentNotFound;
use Psalm\Issue\UndefinedClass;
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic\TNamedObject;
@ -205,12 +206,26 @@ class StaticCallChecker extends \Psalm\Checker\Statements\Expression\CallChecker
) {
continue;
}
} elseif ($lhs_type_part instanceof Type\Atomic\TMixed) {
if (IssueBuffer::accepts(
new InvalidStringClass(
'String cannot be used as a class',
new CodeLocation($statements_checker->getSource(), $stmt)
),
$statements_checker->getSuppressedIssues()
)) {
// fall through
}
continue;
}
if ($lhs_type_part instanceof Type\Atomic\TMixed) {
continue;
}
if (IssueBuffer::accepts(
new InvalidClass(
new UndefinedClass(
'Type ' . $lhs_type_part . ' cannot be called as a class',
new CodeLocation($statements_checker->getSource(), $stmt)
),

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class InvalidStringClass extends CodeIssue
{
}

View File

@ -133,7 +133,7 @@ class AnnotationTest extends TestCase
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidClass
* @expectedExceptionMessage InvalidStringClass
*
* @return void
*/
@ -156,7 +156,7 @@ class AnnotationTest extends TestCase
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidClass
* @expectedExceptionMessage InvalidStringClass
*
* @return void
*/

View File

@ -148,6 +148,9 @@ class DocumentationTest extends TestCase
foreach (self::getCodeBlocksFromDocs() as $issue_name => $blocks) {
switch ($issue_name) {
case 'InvalidStringClass':
continue 2;
case 'InvalidFalsableReturnType':
$ignored_issues = ['FalsableReturnStatement'];
break;

View File

@ -271,13 +271,13 @@ class MethodCallTest extends TestCase
'<?php
$a = 5;
$a::bar();',
'error_message' => 'InvalidClass',
'error_message' => 'UndefinedClass',
],
'intVarNewCall' => [
'<?php
$a = 5;
new $a();',
'error_message' => 'InvalidClass',
'error_message' => 'UndefinedClass',
],
];
}