-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for object and list shape types
- Loading branch information
Showing
12 changed files
with
265 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\PseudoTypes; | ||
|
||
use function implode; | ||
|
||
/** @psalm-immutable */ | ||
final class ListShape extends ArrayShape | ||
{ | ||
public function __toString(): string | ||
{ | ||
return 'list{' . implode(', ', $this->getItems()) . '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\PseudoTypes; | ||
|
||
final class ListShapeItem extends ArrayShapeItem | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\PseudoTypes; | ||
|
||
use phpDocumentor\Reflection\PseudoType; | ||
use phpDocumentor\Reflection\Type; | ||
use phpDocumentor\Reflection\Types\Object_; | ||
|
||
use function implode; | ||
|
||
/** @psalm-immutable */ | ||
final class ObjectShape implements PseudoType | ||
{ | ||
/** @var ObjectShapeItem[] */ | ||
private $items; | ||
|
||
public function __construct(ObjectShapeItem ...$items) | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
/** | ||
* @return ObjectShapeItem[] | ||
*/ | ||
public function getItems(): array | ||
{ | ||
return $this->items; | ||
} | ||
|
||
public function underlyingType(): Type | ||
{ | ||
return new Object_(); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return 'object{' . implode(', ', $this->items) . '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\PseudoTypes; | ||
|
||
final class ObjectShapeItem extends ShapeItem | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\PseudoTypes; | ||
|
||
use phpDocumentor\Reflection\Type; | ||
use phpDocumentor\Reflection\Types\Mixed_; | ||
|
||
use function sprintf; | ||
|
||
abstract class ShapeItem | ||
{ | ||
/** @var string|null */ | ||
private $key; | ||
/** @var Type */ | ||
private $value; | ||
/** @var bool */ | ||
private $optional; | ||
|
||
public function __construct(?string $key, ?Type $value, bool $optional) | ||
{ | ||
$this->key = $key; | ||
$this->value = $value ?? new Mixed_(); | ||
$this->optional = $optional; | ||
} | ||
|
||
public function getKey(): ?string | ||
{ | ||
return $this->key; | ||
} | ||
|
||
public function getValue(): Type | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function isOptional(): bool | ||
{ | ||
return $this->optional; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
if ($this->key !== null) { | ||
return sprintf( | ||
'%s%s: %s', | ||
$this->key, | ||
$this->optional ? '?' : '', | ||
(string) $this->value | ||
); | ||
} | ||
|
||
return (string) $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.