Skip to content
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

refactor: prevent multiple call to accessor when it is a method #162

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/Extractor/ReadAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,28 @@ public function getExpression(Expr\Variable $input): Expr
*
* @see \AutoMapper\Extractor\ReadAccessor::getExtractCallback()
*
* $this->extractCallbacks['method_name']($input)
* $propertyValue ??= $this->extractCallbacks['method_name']($input)
*/
return new Expr\FuncCall(
new Expr\ArrayDimFetch(new Expr\PropertyFetch(new Expr\Variable('this'), 'extractCallbacks'), new Scalar\String_($this->property ?? $this->accessor)),
[
new Arg($input),
]
return new Expr\AssignOp\Coalesce(
new Expr\Variable(name: ($this->property ?? $this->accessor) . 'Value'),
new Expr\FuncCall(
new Expr\ArrayDimFetch(new Expr\PropertyFetch(new Expr\Variable('this'), 'extractCallbacks'), new Scalar\String_($this->property ?? $this->accessor)),
[
new Arg($input),
]
)
);
}

/*
* Use the method call to read the value
*
* $input->method_name(...$args)
* $propertyValue ??= $input->method_name(...$args)
*/
return new Expr\MethodCall($input, $this->accessor, $methodCallArguments);
return new Expr\AssignOp\Coalesce(
new Expr\Variable(name: ($this->property ?? $this->accessor) . 'Value'),
new Expr\MethodCall($input, $this->accessor, $methodCallArguments)
);
}

if (self::TYPE_PROPERTY === $this->type) {
Expand Down
Loading