-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy doc block types from accessors to properties
- Loading branch information
1 parent
5553715
commit b45b598
Showing
7 changed files
with
46 additions
and
30 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
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 |
---|---|---|
@@ -1,17 +1,21 @@ | ||
<?php | ||
namespace AutoValue; | ||
|
||
use phpDocumentor\Reflection\Type; | ||
use Roave\BetterReflection\Reflection\ReflectionMethod; | ||
use Roave\BetterReflection\Reflection\ReflectionProperty; | ||
use Roave\BetterReflection\Reflection\ReflectionType; | ||
|
||
/** | ||
* @author Josh Di Fabio <[email protected]> | ||
*/ | ||
class Property | ||
{ | ||
public static function named(string $name): self | ||
public static function fromAccessorMethod(string $propertyName, ReflectionMethod $accessorMethod): self | ||
{ | ||
$property = new self; | ||
$property->name = $name; | ||
$property->name = $propertyName; | ||
$property->accessorMethod = $accessorMethod; | ||
return $property; | ||
} | ||
|
||
|
@@ -20,26 +24,38 @@ public function name(): string | |
return $this->name; | ||
} | ||
|
||
public function type(): ?ReflectionType | ||
public function phpType(): ?ReflectionType | ||
{ | ||
return $this->type; | ||
if (!$this->phpType) { | ||
$this->phpType = $this->accessorMethod->getReturnType(); | ||
} | ||
return $this->phpType; | ||
} | ||
|
||
public function withType(?ReflectionType $type): self | ||
public function docBlockType(): string | ||
{ | ||
$result = clone $this; | ||
$result->type = $type; | ||
return $result; | ||
if (!isset($this->docBlockType)) { | ||
$docBlockTypes = $this->accessorMethod->getDocBlockReturnTypes(); | ||
$this->docBlockType = $docBlockTypes | ||
? \implode('|', $docBlockTypes) | ||
: ($this->phpType() | ||
? generateTypeHint($this->phpType(), $this->accessorMethod->getDeclaringClass()) | ||
: 'mixed' | ||
); | ||
} | ||
return $this->docBlockType; | ||
} | ||
|
||
public function isRequired(): bool | ||
{ | ||
return $this->type && !$this->type->allowsNull(); | ||
return $this->phpType() && !$this->phpType()->allowsNull(); | ||
} | ||
|
||
private $name; | ||
/** @var ReflectionMethod */ | ||
private $accessorMethod; | ||
/** @var ReflectionType|null */ | ||
private $type; | ||
private $phpType; | ||
|
||
private function __construct() | ||
{ | ||
|
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
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