|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gskema\TypeSniff\Inspection; |
|
4
|
|
|
|
|
5
|
|
|
use Gskema\TypeSniff\Core\Type\Common\ArrayType; |
|
6
|
|
|
use Gskema\TypeSniff\Core\Type\Common\UndefinedType; |
|
7
|
|
|
use Gskema\TypeSniff\Core\Type\Declaration\NullableType; |
|
8
|
|
|
use Gskema\TypeSniff\Core\Type\DocBlock\NullType; |
|
9
|
|
|
use Gskema\TypeSniff\Core\Type\TypeConverter; |
|
10
|
|
|
use Gskema\TypeSniff\Inspection\Subject\AbstractTypeSubject; |
|
11
|
|
|
use Gskema\TypeSniff\Inspection\Subject\PropTypeSubject; |
|
12
|
|
|
|
|
13
|
|
|
class FnTypeInspector |
|
14
|
|
|
{ |
|
15
|
16 |
|
public static function reportReplaceableTypes(AbstractTypeSubject $subject): void |
|
16
|
|
|
{ |
|
17
|
|
|
// (string $arg1 = null) -> (?string $arg1 = null) |
|
18
|
|
|
if ( |
|
19
|
16 |
|
$subject->getValueType() instanceof NullType |
|
20
|
16 |
|
&& !($subject->getFnType() instanceof UndefinedType) |
|
21
|
16 |
|
&& !($subject->getFnType() instanceof NullableType) |
|
22
|
|
|
) { |
|
23
|
2 |
|
$subject->addFnTypeWarning(sprintf( |
|
24
|
2 |
|
'Change :subject: type declaration to nullable, e.g. %s. Remove default null value if this argument is required.', |
|
25
|
2 |
|
(new NullableType($subject->getFnType()))->toString() |
|
26
|
|
|
)); |
|
27
|
|
|
} |
|
28
|
16 |
|
} |
|
29
|
|
|
|
|
30
|
16 |
|
public static function reportSuggestedTypes(AbstractTypeSubject $subject): void |
|
31
|
|
|
{ |
|
32
|
16 |
|
if ($subject->hasDefinedFnType()) { |
|
33
|
11 |
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
16 |
|
$isProp = $subject instanceof PropTypeSubject; |
|
37
|
16 |
|
$valueType = $subject->getValueType(); |
|
38
|
16 |
|
$hasDefaultValue = $valueType && !($valueType instanceof UndefinedType); |
|
39
|
16 |
|
$hasDocType = $subject->hasDefinedDocType() || $subject->hasAttribute('ArrayShape'); |
|
40
|
|
|
|
|
41
|
16 |
|
$requestAnyType = false; |
|
42
|
16 |
|
if ($subject->hasDefinedDocType()) { |
|
43
|
11 |
|
$possibleFnType = TypeConverter::toExampleFnType($subject->getDocType(), $isProp); |
|
44
|
10 |
|
} elseif ($subject->hasAttribute('ArrayShape')) { |
|
45
|
1 |
|
$possibleFnType = new ArrayType(); |
|
46
|
|
|
} else { |
|
47
|
|
|
// Doc type and ArrayShape undefined, require any type decl. or suggest from value type. |
|
48
|
9 |
|
$possibleFnType = $hasDefaultValue ? TypeConverter::toExampleFnType($valueType, $isProp) : null; |
|
49
|
9 |
|
$requestAnyType = true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
16 |
|
if ($possibleFnType || $requestAnyType) { |
|
53
|
15 |
|
$warning = 'Add type declaration for :subject:'; |
|
54
|
15 |
|
$warning .= $possibleFnType ? sprintf(', e.g.: "%s"', $possibleFnType->toString()) : ''; |
|
55
|
15 |
|
$warning .= $hasDocType ? '' : ' or create PHPDoc with type hint'; |
|
56
|
15 |
|
$warning .= $isProp && !$hasDefaultValue ? '. Add default value or keep property in an uninitialized state.' : '.'; |
|
57
|
15 |
|
$subject->addFnTypeWarning($warning); |
|
58
|
|
|
} |
|
59
|
16 |
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|