Skip to content

Commit

Permalink
Merge e3fd875 into fe3da54
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-i committed Sep 2, 2021
2 parents fe3da54 + e3fd875 commit 0195d20
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
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

0 comments on commit 0195d20

Please sign in to comment.