-
Notifications
You must be signed in to change notification settings - Fork 0
Blueprint building #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gardziejewskik
wants to merge
33
commits into
master
Choose a base branch
from
blueprint-building
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
9bad536
Draft of object builder based on callable blueprint
gardziejewskik 1c0f4b2
Building complex object without default values
gardziejewskik 985eb59
Building complex object with default values
gardziejewskik 8e4bbb6
Building object with default values fix
gardziejewskik d1957bc
Building advanced object hierarchy with nested objects
gardziejewskik 8326c72
Handle all ReflectionBuilder cases with BlueprintBuilder
gardziejewskik e6941b0
Removed unused dependency
gardziejewskik aa5c23e
Add code standard analyse
gardziejewskik 580d2fa
Fix coding standards
gardziejewskik ab165fe
Refactoring and code cleaning
gardziejewskik f423631
Fix travis
gardziejewskik 20b5e32
Fix travis
gardziejewskik b9b3db2
Fix travis
gardziejewskik ff97ad3
Refactor with remove unnecessary dependency
gardziejewskik ca15bfe
Fix
gardziejewskik 83d4fbe
Small refactor CodeGenerator stack
gardziejewskik 7e414c4
Refactor to use serializer for Node
gardziejewskik a8a5991
CS fix
gardziejewskik 45d5461
FilesystemStore implementation
gardziejewskik 9babfeb
Improve uTests
gardziejewskik d22d289
Merge from origin/master
gardziejewskik c11de98
Resolve conflicts
gardziejewskik 5c22a4a
Improve quality of tests
gardziejewskik 4049d30
Change PhpStan DocParser interface and unit improvement
gardziejewskik 94a9f37
Better segregation of object for testing
gardziejewskik 9b48cf4
Move some tests to component
gardziejewskik 4e152a3
Fix CS
gardziejewskik bfb49a7
PhpDocParser tests
gardziejewskik 30589be
Improve CodeCoverage
gardziejewskik 029d3cc
Add StoreDecorator for PatternGenerator
gardziejewskik 73aa8d2
Different parameter name strategy for Blueprint
gardziejewskik 48f08e6
Fixes after CR
gardziejewskik ce5f444
Refactor - rename
gardziejewskik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 +1,7 @@ | ||
| composer.lock | ||
| vendor | ||
| vendor | ||
| .idea | ||
| .php_cs.cache | ||
| .phpcs.cache | ||
| data | ||
| test/result |
This file contains hidden or 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 hidden or 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 hidden or 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,6 @@ | ||
| parameters: | ||
| tmpDir: data/cache/phpstan-test | ||
| includes: | ||
| - vendor/phpstan/phpstan-phpunit/extension.neon | ||
| - vendor/phpstan/phpstan-phpunit/rules.neon | ||
| - vendor/phpstan/phpstan-strict-rules/rules.neon |
This file contains hidden or 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,4 @@ | ||
| includes: | ||
| - vendor/phpstan/phpstan-strict-rules/rules.neon | ||
| parameters: | ||
| tmpDir: data/cache/phpstan |
This file contains hidden or 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 hidden or 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,45 @@ | ||
| <?xml version="1.0"?> | ||
| <ruleset name="ObjectBuilder"> | ||
| <rule ref="PSR2"/> | ||
| <rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/ruleset.xml"> | ||
| <exclude name="SlevomatCodingStandard.Classes.UnusedPrivateElements"/> | ||
| <exclude name="SlevomatCodingStandard.Commenting.DisallowOneLinePropertyDocComment"/> | ||
| <exclude name="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/> | ||
| <exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation"/> | ||
| <exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/> | ||
| <exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions"/> | ||
| <exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions"/> | ||
| <exclude name="SlevomatCodingStandard.Namespaces.UseOnlyWhitelistedNamespaces"/> | ||
| <exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingPropertyTypeHint"/> | ||
| <exclude name="SlevomatCodingStandard.Operators.DisallowIncrementAndDecrementOperators"/> | ||
| </rule> | ||
| <rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName"> | ||
| <properties> | ||
| <property name="rootNamespaces" type="array" value=" | ||
| src=>RstGroup\ObjectBuilder, | ||
| test=>RstGroup\ObjectBuilder\Test, | ||
| "/> | ||
| </properties> | ||
| </rule> | ||
| <rule ref="SlevomatCodingStandard.Types.EmptyLinesAroundTypeBraces"> | ||
| <properties> | ||
| <property name="linesCountAfterOpeningBrace" type="int" value="0"/> | ||
| <property name="linesCountBeforeClosingBrace" type="int" value="0"/> | ||
| </properties> | ||
| </rule> | ||
| <rule ref="SlevomatCodingStandard.Commenting.ForbiddenAnnotations"> | ||
| <properties> | ||
| <property name="forbiddenAnnotations" type="array" value="@author,@created,@version,@package,@copyright,@license" /> | ||
| </properties> | ||
| </rule> | ||
| <rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration"> | ||
| <properties> | ||
| <property name="allAnnotationsAreUseful" type="boolean" value="true" /> | ||
| </properties> | ||
| </rule> | ||
| <rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"> | ||
| <properties> | ||
| <property name="searchAnnotations" type="boolean" value="true" /> | ||
| </properties> | ||
| </rule> | ||
| </ruleset> |
This file contains hidden or 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,8 +1,9 @@ | ||
| <?php declare(strict_types=1); | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RstGroup\ObjectBuilder; | ||
|
|
||
| interface Builder | ||
| { | ||
| /** @param mixed[] $data */ | ||
| public function build(string $class, array $data): object; | ||
| } |
This file contains hidden or 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 RstGroup\ObjectBuilder\Builder; | ||
|
|
||
| use RstGroup\ObjectBuilder\Builder; | ||
|
|
||
| final class Blueprint implements Builder | ||
| { | ||
| /** @var Builder\Blueprint\Factory */ | ||
| private $blueprintFactory; | ||
| /** @var ParameterNameStrategy */ | ||
| private $strategy; | ||
|
|
||
| /** @codeCoverageIgnore */ | ||
| public function __construct( | ||
| Builder\Blueprint\Factory $factory, | ||
| ParameterNameStrategy $strategy | ||
| ) { | ||
| $this->blueprintFactory = $factory; | ||
| $this->strategy = $strategy; | ||
| } | ||
|
|
||
| /** @param mixed[] $data */ | ||
| public function build(string $class, array $data): object | ||
| { | ||
| $blueprint = $this->blueprintFactory->create($class); | ||
|
|
||
| $preparedData = $this->prepareData($data); | ||
|
|
||
| return $blueprint($preparedData); | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed[] $data | ||
| * @return mixed[] | ||
| */ | ||
| private function prepareData(array $data): array | ||
| { | ||
| $preparedData = []; | ||
|
|
||
| foreach ($data as $key => $value) { | ||
| if (is_array($value)) { | ||
| $value = $this->prepareData($value); | ||
| } | ||
|
|
||
| if (!is_int($key)) { | ||
| $key = $this->strategy->getName($key); | ||
| } | ||
|
|
||
| $preparedData[$key] = $value; | ||
| } | ||
|
|
||
| return $preparedData; | ||
| } | ||
| } | ||
This file contains hidden or 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,8 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RstGroup\ObjectBuilder\Builder\Blueprint; | ||
|
|
||
| interface Factory | ||
| { | ||
| public function create(string $class): callable; | ||
| } |
This file contains hidden or 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,36 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RstGroup\ObjectBuilder\Builder\Blueprint\Factory; | ||
|
|
||
| use RstGroup\ObjectBuilder\Builder\Blueprint\Factory; | ||
| use RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Pattern\Generator; | ||
| use RstGroup\ObjectBuilder\BuildingError; | ||
|
|
||
| final class CodeGenerator implements Factory | ||
| { | ||
| /** @var Generator */ | ||
| private $generator; | ||
|
|
||
| /** @codeCoverageIgnore */ | ||
| public function __construct(Generator $generator) | ||
| { | ||
| $this->generator = $generator; | ||
| } | ||
|
|
||
| public function create(string $class): callable | ||
| { | ||
| $pattern = $this->generator->create($class); | ||
|
|
||
| $blueprint = eval($pattern); | ||
| if (! is_callable($blueprint)) { | ||
| throw new BuildingError( | ||
| sprintf( | ||
| 'Generated blueprint is not valid %s', | ||
| (string) $blueprint | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return $blueprint; | ||
| } | ||
| } |
This file contains hidden or 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,54 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator; | ||
|
|
||
| abstract class Node | ||
| { | ||
| /** @var string */ | ||
| protected $name; | ||
| /** @var mixed|null */ | ||
| private $defaultValue; | ||
| /** @var string */ | ||
| private $type; | ||
| /** @var bool */ | ||
| private $nullable; | ||
|
|
||
| /** @param mixed $defaultValue */ | ||
| public function __construct(string $type, string $name, bool $nullable, $defaultValue) | ||
| { | ||
| $this->name = $name; | ||
| $this->defaultValue = $defaultValue; | ||
| $this->type = $type; | ||
| $this->nullable = $nullable; | ||
| } | ||
|
|
||
| public function type(): string | ||
| { | ||
| return $this->type; | ||
| } | ||
|
|
||
| public function name(): string | ||
| { | ||
| return $this->name; | ||
| } | ||
|
|
||
| public function hasDefaultValue(): bool | ||
| { | ||
| if ($this->nullable()) { | ||
| return true; | ||
| } | ||
|
|
||
| return null !== $this->defaultValue; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Null it's still value |
||
| } | ||
|
|
||
| public function nullable(): bool | ||
| { | ||
| return $this->nullable; | ||
| } | ||
|
|
||
| /** @return mixed */ | ||
| public function defaultValue() | ||
| { | ||
| return $this->defaultValue; | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/Builder/Blueprint/Factory/CodeGenerator/Node/Composite.php
This file contains hidden or 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,22 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| use RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| final class Composite extends Node | ||
| { | ||
| /** @var Node[] */ | ||
| private $nodes = []; | ||
|
|
||
| public function add(Node $node): void | ||
| { | ||
| $this->nodes[] = $node; | ||
| } | ||
|
|
||
| /** @return Node[] */ | ||
| public function innerNodes(): iterable | ||
| { | ||
| return $this->nodes; | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/Builder/Blueprint/Factory/CodeGenerator/Node/ObjectList.php
This file contains hidden or 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,27 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| use RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| final class ObjectList extends Node | ||
| { | ||
| /** @var Node */ | ||
| private $objectNode; | ||
|
|
||
| public function __construct(string $name, Node $objectNode) | ||
| { | ||
| parent::__construct( | ||
| $objectNode->type(), | ||
| $name, | ||
| false, | ||
| null | ||
| ); | ||
| $this->objectNode = $objectNode; | ||
| } | ||
|
|
||
| public function objectNode(): Node | ||
| { | ||
| return $this->objectNode; | ||
| } | ||
| } |
This file contains hidden or 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 RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| use RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| final class Scalar extends Node | ||
| { | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/Builder/Blueprint/Factory/CodeGenerator/Node/Serializer.php
This file contains hidden or 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 RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| use RstGroup\ObjectBuilder\Builder\Blueprint\Factory\CodeGenerator\Node; | ||
|
|
||
| interface Serializer | ||
| { | ||
| public function serialize(Node $node): string; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Key can any, not only int or string
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The key can either be an integer or a string. The value can be of any type.
Other types are casted underlying to string or int.