Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tables
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
grifart libs
tables
Commits
5acfddf4
Verified
Commit
5acfddf4
authored
3 years ago
by
Jiří Pudil
Browse files
Options
Downloads
Patches
Plain Diff
generate array type in reconstitute()
parent
1cc222e0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!15
generate array type in reconstitute()
Pipeline
#36753
passed
3 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Scaffolding/ReconstituteConstructor.php
+13
-6
13 additions, 6 deletions
src/Scaffolding/ReconstituteConstructor.php
with
13 additions
and
6 deletions
src/Scaffolding/ReconstituteConstructor.php
+
13
−
6
View file @
5acfddf4
...
...
@@ -5,8 +5,7 @@ namespace Grifart\Tables\Scaffolding;
use
Grifart\ClassScaffolder\Capabilities\Capability
;
use
Grifart\ClassScaffolder\ClassInNamespace
;
use
Grifart\ClassScaffolder\Definition\ClassDefinition
;
use
Grifart\ClassScaffolder\Definition\Field
;
use
Nette\PhpGenerator
as
Code
;
use
Nette\PhpGenerator\PhpLiteral
;
final
class
ReconstituteConstructor
implements
Capability
{
...
...
@@ -20,13 +19,21 @@ final class ReconstituteConstructor implements Capability
$reconstitute
=
$classType
->
addMethod
(
'reconstitute'
)
->
setReturnType
(
'static'
)
->
setParameters
([(
new
Code\Parameter
(
'values'
))
->
setTypeHint
(
'array'
)])
->
setStatic
();
$reconstitute
->
addParameter
(
'values'
)
->
setTypeHint
(
'array'
);
$fields
=
$definition
->
getFields
();
$questionMarks
=
\implode
(
', '
,
array_fill
(
0
,
\count
(
$fields
),
'?'
));
$literals
=
\array_map
(
function
(
Field
$field
):
Code
\PhpLiteral
{
return
new
Code\PhpLiteral
(
"
\$
values['"
.
$field
->
getName
()
.
"']"
);},
$fields
);
$shapeFields
=
$literals
=
[];
foreach
(
$fields
as
$field
)
{
$name
=
$field
->
getName
();
$type
=
$field
->
getType
();
$shapeFields
[]
=
\sprintf
(
'%s: %s'
,
$name
,
$type
->
getDocCommentType
(
$draft
->
getNamespace
()));
$literals
[]
=
new
PhpLiteral
(
"
\$
values['"
.
$name
.
"']"
);
}
$reconstitute
->
addBody
(
"return new static(
$questionMarks
);"
,
\array_values
(
$literals
));
$reconstitute
->
addComment
(
\sprintf
(
'@param array{%s} $values'
,
\implode
(
', '
,
$shapeFields
)));
$reconstitute
->
addBody
(
"return new static(...?);"
,
[
$literals
]);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment