-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from BambooHR/jgardiner/readonly-improvements
Improve support for readonly properties.
- Loading branch information
Showing
31 changed files
with
306 additions
and
198 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
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
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,32 @@ | ||
<?php | ||
|
||
namespace BambooHR\Guardrail\Checks; | ||
|
||
use BambooHR\Guardrail\Scope; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\PropertyProperty; | ||
|
||
class ReadOnlyPropertyCheck extends BaseCheck { | ||
function getCheckNodeTypes() { | ||
return [PropertyProperty::class]; | ||
} | ||
|
||
public function run($fileName, Node $node, ClassLike $inside = null, Scope $scope = null) { | ||
if ($node instanceof PropertyProperty) { | ||
$parentNodes = $scope->getParentNodes(); | ||
|
||
/** @var Node\Stmt\Property $prop */ | ||
$prop = end($parentNodes); | ||
$isReadOnlyClass = $inside instanceof Node\Stmt\Class_ && $inside->isReadonly(); | ||
if ($prop->isReadonly() || $isReadOnlyClass) { | ||
if ($node->default !== null) { | ||
$this->emitError($fileName, $node, ErrorConstants::TYPE_READONLY_DECLARATION, "Readonly properties can't have a default value"); | ||
} | ||
if ($prop->type === null) { | ||
$this->emitError($fileName, $node, ErrorConstants::TYPE_READONLY_DECLARATION, "Readonly properties must have a declared type"); | ||
} | ||
} | ||
} | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace BambooHR\Guardrail; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Builder\Property; | ||
use PhpParser\Builder\Param; | ||
|
||
class EnumCodeAugmenter { | ||
static public function addEnumPropsAndMethods(Node\Stmt\Enum_ $enum) { | ||
$isBacked = !is_null($enum->scalarType); | ||
$property = new Property("name"); | ||
$property->setType(new Node\Identifier("string")); | ||
$property->makeReadonly(); | ||
$enum->stmts[]= $property->getNode(); | ||
if ($isBacked) { | ||
$enum->stmts[] = new Node\Stmt\ClassMethod("values",["returnType"=>"array"]); | ||
$property = new Property("value"); | ||
$property->makeReadonly(); | ||
$property->setType( $enum->scalarType ); | ||
$enum->stmts[]=$property->getNode(); | ||
|
||
$enumName = $enum->namespacedName->toString(); | ||
$param = (new Param("fromValue"))->setType($enum->scalarType); | ||
$enum->stmts[]=new Node\Stmt\ClassMethod("tryFrom",["returnType" => $enumName, "flags"=>Node\Stmt\Class_::MODIFIER_STATIC, 'params'=>[$param->getNode()]]); | ||
$enum->stmts[]=new Node\Stmt\ClassMethod("from", ["returnType" => $enumName, "flags"=>Node\Stmt\Class_::MODIFIER_STATIC, 'params'=>[$param->getNode()]]); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.