Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support list type #12

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/PhpDocTypeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpDocTypeReader\Type\FloatType;
use PhpDocTypeReader\Type\GenericType;
use PhpDocTypeReader\Type\IntType;
use PhpDocTypeReader\Type\ListType;
use PhpDocTypeReader\Type\MixedType;
use PhpDocTypeReader\Type\NullType;
use PhpDocTypeReader\Type\ObjectType;
Expand Down Expand Up @@ -120,13 +121,22 @@ private function getTypeFromNodeType(TypeNode $type_node, IdentifierContext $ide
return new NullType();
case 'array':
return new ArrayType(new MixedType());
case 'list':
return new ListType(new MixedType());
default:
return new ObjectType(
$this->tryGetClassNameFromIdentifier($type_node, $identifier_context)
);
}
}
if ($type_node instanceof GenericTypeNode) {
if ($type_node->type->name === 'list') {
if (count($type_node->genericTypes) !== 1) {
throw new \LogicException('unsupported parameter types of list type');
}
$type = $this->getTypeFromNodeType($type_node->genericTypes[0], $identifier_context);
return new ListType($type);
}
if ($type_node->type->name === 'array') {
if (count($type_node->genericTypes) === 1) {
$type = $this->getTypeFromNodeType($type_node->genericTypes[0], $identifier_context);
Expand Down
24 changes: 24 additions & 0 deletions src/Type/ListType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of the sj-i/phpdoc-type-reader package.
*
* (c) sji <sji@sj-i.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpDocTypeReader\Type;

class ListType extends ArrayType
{
public function __construct(Type $value_type)
{
$this->key_type = new IntType();
$this->value_type = $value_type;
$this->parameter_types = [$value_type, $this->key_type];
}
}
15 changes: 15 additions & 0 deletions tests/PhpDocTypeReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PhpDocTypeReader\Type\FloatType;
use PhpDocTypeReader\Type\GenericType;
use PhpDocTypeReader\Type\IntType;
use PhpDocTypeReader\Type\ListType;
use PhpDocTypeReader\Type\MixedType;
use PhpDocTypeReader\Type\NullType;
use PhpDocTypeReader\Type\ObjectType;
Expand Down Expand Up @@ -262,6 +263,20 @@ public function paramProvider()
'/** @param int[] $array_var */',
$default_identifier_context,
],
[
[
'list_var' => new ListType(new IntType()),
],
'/** @param list<int> $list_var */',
$default_identifier_context,
],
[
[
'untyped_list_var' => new ListType(new MixedType()),
],
'/** @param list $untyped_list_var */',
$default_identifier_context,
],
[
[
'union_var' => new UnionType([new IntType(), new StringType()]),
Expand Down