Skip to content

Commit

Permalink
feat: add support for class-string doc type
Browse files Browse the repository at this point in the history
  • Loading branch information
gskema committed Jun 26, 2023
1 parent 7d1527e commit 542b925
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to `phpcs-type-sniff` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 81.3.0 - 2023-06-26
### Added
- Support for `class-string` doc type

## 81.2.0 - 2023-04-07
### Changed
- Remove upper bound PHP requirement so package can be installed on higher PHP versions even if locked.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class Banana
#[ArrayShape(['foo' => 'int'])]
public $prop14 = ['foo' => 1]; // ArrayShape supported

/** @var class-string */
public string $prop15;

public function __construct(
$param1, // missing param type decl. in method PHPDoc
public $param2, // missing param type decl. (in method PHPDoc or inline PHPDoc)
Expand Down
13 changes: 13 additions & 0 deletions src/Core/Type/DocBlock/ClassStringType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Gskema\TypeSniff\Core\Type\DocBlock;

use Gskema\TypeSniff\Core\Type\TypeInterface;

class ClassStringType implements TypeInterface
{
public function toString(): string
{
return 'class-string';
}
}
5 changes: 5 additions & 0 deletions src/Core/Type/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use Gskema\TypeSniff\Core\Type\Common\ParentType;
use Gskema\TypeSniff\Core\Type\Common\SelfType;
use Gskema\TypeSniff\Core\Type\Common\StaticType;
use Gskema\TypeSniff\Core\Type\Common\StringType;
use Gskema\TypeSniff\Core\Type\Common\UndefinedType;
use Gskema\TypeSniff\Core\Type\Common\UnionType;
use Gskema\TypeSniff\Core\Type\Declaration\NullableType;
use Gskema\TypeSniff\Core\Type\DocBlock\ClassStringType;
use Gskema\TypeSniff\Core\Type\DocBlock\DoubleType;
use Gskema\TypeSniff\Core\Type\DocBlock\ThisType;
use Gskema\TypeSniff\Core\Type\DocBlock\TrueType;
Expand Down Expand Up @@ -99,6 +101,9 @@ class TypeComparator
IterableType::class,
FqcnType::class, // e.g. Collection|Image[]
],
ClassStringType::class => [
StringType::class,
],
// bool does not cover true|false - fn type is concrete and specified - copy it to PHPDoc pls
];

Expand Down
3 changes: 3 additions & 0 deletions src/Core/Type/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
use Gskema\TypeSniff\Core\Type\Common\NeverType;
use Gskema\TypeSniff\Core\Type\Common\NullType;
use Gskema\TypeSniff\Core\Type\Common\StaticType;
use Gskema\TypeSniff\Core\Type\Common\StringType;
use Gskema\TypeSniff\Core\Type\Common\UndefinedType;
use Gskema\TypeSniff\Core\Type\Common\UnionType;
use Gskema\TypeSniff\Core\Type\Common\VoidType;
use Gskema\TypeSniff\Core\Type\Declaration\NullableType;
use Gskema\TypeSniff\Core\Type\DocBlock\ClassStringType;
use Gskema\TypeSniff\Core\Type\DocBlock\DoubleType;
use Gskema\TypeSniff\Core\Type\DocBlock\ResourceType;
use Gskema\TypeSniff\Core\Type\DocBlock\ThisType;
Expand Down Expand Up @@ -118,6 +120,7 @@ public static function toExampleFnType(TypeInterface $docType, bool $isProp): ?T
TrueType::class => BoolType::class,
TypedArrayType::class => ArrayType::class,
ResourceType::class => null,
ClassStringType::class => StringType::class,
];

$docTypeClass = get_class($docType);
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Gskema\TypeSniff\Core\Type\Common\UnionType;
use Gskema\TypeSniff\Core\Type\Common\VoidType;
use Gskema\TypeSniff\Core\Type\Declaration\NullableType;
use Gskema\TypeSniff\Core\Type\DocBlock\ClassStringType;
use Gskema\TypeSniff\Core\Type\DocBlock\DoubleType;
use Gskema\TypeSniff\Core\Type\DocBlock\ResourceType;
use Gskema\TypeSniff\Core\Type\DocBlock\ThisType;
Expand All @@ -39,6 +40,7 @@ class TypeFactory
'array' => ArrayType::class,
'bool' => BoolType::class,
'boolean' => BoolType::class,
'class-string' => ClassStringType::class,
'callable' => CallableType::class,
'double' => DoubleType::class,
'false' => FalseType::class,
Expand Down
6 changes: 6 additions & 0 deletions tests/Core/Type/TypeComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public function dataCompare(): array

59 => ['int|null', 'int|null|false', '', '', 'false'],
60 => ['int|null|bool', 'int|null|false', '', 'bool', 'false'],

61 => ['class-string', 'string', '', '', ''],
62 => ['class-string|string', 'string', '', '', ''],
63 => ['class-string|string|int', 'string', '', 'int', ''],

// doc_type, fn_type, val_type, wrong_doc, missing_doc
];
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Core/Type/TypeConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Gskema\TypeSniff\Core\Type\Common\UnionType;
use Gskema\TypeSniff\Core\Type\Common\VoidType;
use Gskema\TypeSniff\Core\Type\Declaration\NullableType;
use Gskema\TypeSniff\Core\Type\DocBlock\ClassStringType;
use Gskema\TypeSniff\Core\Type\DocBlock\DoubleType;
use Gskema\TypeSniff\Core\Type\DocBlock\ResourceType;
use Gskema\TypeSniff\Core\Type\DocBlock\ThisType;
Expand Down Expand Up @@ -179,6 +180,10 @@ public function dataToExampleFnType(): array
new UnionType([new FalseType(), new CallableType()]),
false
],
34 => [
new ClassStringType(),
new StringType(),
],
];
}

Expand Down
15 changes: 14 additions & 1 deletion tests/Core/Type/TypeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Gskema\TypeSniff\Core\Type\Common\UnionType;
use Gskema\TypeSniff\Core\Type\Common\VoidType;
use Gskema\TypeSniff\Core\Type\Declaration\NullableType;
use Gskema\TypeSniff\Core\Type\DocBlock\ClassStringType;
use Gskema\TypeSniff\Core\Type\DocBlock\DoubleType;
use Gskema\TypeSniff\Core\Type\DocBlock\ResourceType;
use Gskema\TypeSniff\Core\Type\DocBlock\ThisType;
Expand Down Expand Up @@ -71,7 +72,15 @@ public function dataSplit(): array
[
'(int|array<int, string & int>) | bool & string|int|array(int, int) | Desc Desc2',
[['(int|array<int, string & int>)', 'bool & string', 'int', 'array(int, int)', 'Desc'], 'Desc2'],
]
],
[
'class-string',
[['class-string'], '']
],
[
'class-string|int',
[['class-string', 'int'], '']
],
];

return $dataSets;
Expand Down Expand Up @@ -257,6 +266,10 @@ public function dataFromRawType(): array
new IntersectionType([new FqcnType('B'), new FqcnType('D')]),
]),
],
[
'class-string',
new ClassStringType(),
]
];

return $dataSets;
Expand Down
3 changes: 3 additions & 0 deletions tests/Sniffs/CompositeCodeElementSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ public function dataProcess(): array
'047 Missing "string" type in CONST2 constant type hint',
'052 Intersection types can only contain class or interfaces names',
'056 Add type declaration for parameter $param1, e.g.: "Countable&IteratorAggregate".',
'073 Add type declaration for parameter $prop1, e.g.: "string".',
'073 Add type declaration for return value, e.g.: "string".',
'078 Add type declaration for property $prop4, e.g.: "string". Add default value or keep property in an uninitialized state.',
],
];

Expand Down
19 changes: 19 additions & 0 deletions tests/Sniffs/fixtures/TestClass14.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,23 @@ public function method6(
\Iterator&\Countable $prop2,
$param3
): never { exit; }

/**
* @param class-string $prop1
* @return class-string
*/
public function method7(string $prop1): string
{
}

/**
* @param class-string $prop1
* @return class-string
*/
public function method8($prop1)
{
}

/** @var class-string */
public $prop4;
}

0 comments on commit 542b925

Please sign in to comment.