-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rules: provide phpdoc-compatible type for rule input and output
- Loading branch information
Showing
53 changed files
with
1,906 additions
and
20 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,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\ObjectMapper\PhpTypes; | ||
|
||
use function array_key_last; | ||
|
||
final class ClassReferenceNode implements Node | ||
{ | ||
|
||
/** @var class-string */ | ||
private string $class; | ||
|
||
/** @var array<int|string, Node> */ | ||
private array $structure; | ||
|
||
/** | ||
* @param class-string $class | ||
* @param array<int|string, Node> $structure | ||
*/ | ||
public function __construct(string $class, array $structure) | ||
{ | ||
$this->class = $class; | ||
$this->structure = $structure; | ||
} | ||
|
||
public function getArrayShape(): string | ||
{ | ||
$inline = ''; | ||
$lastKey = array_key_last($this->structure); | ||
foreach ($this->structure as $field => $node) { | ||
$inline .= | ||
$field | ||
. ': ' | ||
. ((string) $node); | ||
|
||
if ($field !== $lastKey) { | ||
$inline .= ', '; | ||
} | ||
} | ||
|
||
return "array{{$inline}}"; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->class; | ||
} | ||
|
||
} |
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,55 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\ObjectMapper\PhpTypes; | ||
|
||
use function array_key_last; | ||
|
||
final class CompoundNode implements Node | ||
{ | ||
|
||
/** @var array<int, Node> */ | ||
private array $nodes; | ||
|
||
private string $operator; | ||
|
||
/** | ||
* @param array<int, Node> $nodes | ||
*/ | ||
private function __construct(array $nodes, string $operator) | ||
{ | ||
$this->nodes = $nodes; | ||
$this->operator = $operator; | ||
} | ||
|
||
/** | ||
* @param array<int, Node> $nodes | ||
*/ | ||
public static function createAndType(array $nodes): self | ||
{ | ||
return new self($nodes, '&'); | ||
} | ||
|
||
/** | ||
* @param array<int, Node> $nodes | ||
*/ | ||
public static function createOrType(array $nodes): self | ||
{ | ||
return new self($nodes, '|'); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
$string = ''; | ||
$lastKey = array_key_last($this->nodes); | ||
foreach ($this->nodes as $key => $node) { | ||
$string .= $node; | ||
|
||
if ($key !== $lastKey) { | ||
$string .= $this->operator; | ||
} | ||
} | ||
|
||
return "($string)"; | ||
} | ||
|
||
} |
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 Orisai\ObjectMapper\PhpTypes; | ||
|
||
use function is_bool; | ||
use function is_float; | ||
use function is_int; | ||
use function var_export; | ||
|
||
final class LiteralNode implements Node | ||
{ | ||
|
||
/** @var int|float|string|bool|null */ | ||
private $value; | ||
|
||
/** | ||
* @param int|float|string|bool|null $value | ||
*/ | ||
public function __construct($value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
if (is_bool($this->value)) { | ||
return $this->value ? 'true' : 'false'; | ||
} | ||
|
||
if ($this->value === null) { | ||
return 'null'; | ||
} | ||
|
||
if (is_int($this->value) || is_float($this->value)) { | ||
return var_export($this->value, true); | ||
} | ||
|
||
return "'{$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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\ObjectMapper\PhpTypes; | ||
|
||
final class MultiValueNode implements Node | ||
{ | ||
|
||
private string $name; | ||
|
||
private ?Node $key; | ||
|
||
private Node $item; | ||
|
||
public function __construct(string $name, ?Node $key, Node $item) | ||
{ | ||
$this->name = $name; | ||
$this->key = $key; | ||
$this->item = $item; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->name | ||
. '<' | ||
. ($this->key !== null ? "$this->key, " : '') | ||
. ((string) $this->item) | ||
. '>'; | ||
} | ||
|
||
} |
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,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\ObjectMapper\PhpTypes; | ||
|
||
interface Node | ||
{ | ||
|
||
public function __toString(): string; | ||
|
||
} |
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,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Orisai\ObjectMapper\PhpTypes; | ||
|
||
final class SimpleNode implements Node | ||
{ | ||
|
||
/** @var non-empty-string */ | ||
private string $value; | ||
|
||
/** | ||
* @param non-empty-string $value | ||
*/ | ||
public function __construct(string $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $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
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.