Skip to content

Commit

Permalink
Support laminas code ^4.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Jul 3, 2024
1 parent 59a5c10 commit 4a64771
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 120 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"azjezz/psl": "^2.1",
"laminas/laminas-code": "^4.14.0",
"laminas/laminas-code": "^4.5.0",
"php-soap/cached-engine": "~0.2",
"php-soap/engine": "^2.10.1",
"php-soap/encoding": "~0.4",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ parameters:
ignoreErrors:
# This is something we hope to fix in PHP 7.4
# https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters
- '#Call to an undefined method Phpro\\.*(Context|RuleSet)Interface.*$#'
- '#Call to an undefined method Phpro\\.*(Context|RuleSet)Interface.*$#'#
18 changes: 12 additions & 6 deletions src/Phpro/SoapClient/CodeGenerator/Assembler/ClassMapAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ public function assemble(ContextInterface $context)
$linefeed = $file::LINE_FEED;
$classMap = $this->assembleClassMap($typeMap, $linefeed, $file->getIndentation());
$code = $this->assembleClassMapCollection($classMap, $linefeed).$linefeed;
$class->addMethodFromGenerator(
(new MethodGenerator('getCollection'))
->setStatic(true)
->setBody('return '.$code)
->setReturnType(ClassMapCollection::class)
);
$class->addMethodFromGenerator($this->generateGetCollectionMethod($code));
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}
Expand Down Expand Up @@ -104,4 +99,15 @@ private function assembleClassMapCollection(string $classMap, string $linefeed):

return sprintf(implode($linefeed, $code), $classMap);
}

private function generateGetCollectionMethod(string $code): MethodGenerator
{
$method = new MethodGenerator('getCollection');

$method->setStatic(true);
$method->setBody('return ' . $code);
$method->setReturnType(ClassMapCollection::class);

return $method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,44 @@ public function assemble(ContextInterface $context)
$class = $context->getClass();
try {
$caller = $this->generateClassNameAndAddImport(Caller::class, $class);
$class->addPropertyFromGenerator(
(new PropertyGenerator('caller'))
->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE)
->omitDefaultValue(true)
->setDocBlock((new DocBlockGenerator())
->setWordWrap(false)
->setTags([
[
'name' => 'var',
'description' => $caller,
],
])),
);
$class->addMethodFromGenerator(
(new MethodGenerator('__construct'))
->setParameter(new ParameterGenerator('caller', Caller::class))
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC)
->setBody('$this->caller = $caller;')
);
$class->addPropertyFromGenerator($this->generateCallerProperty($caller));
$class->addMethodFromGenerator($this->generateConstructor());
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}

return true;
}

private function generateConstructor(): MethodGenerator
{
$method = new MethodGenerator('__construct');

$method->setParameter(new ParameterGenerator('caller', Caller::class));
$method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$method->setBody('$this->caller = $caller;');

return $method;
}

private function generateCallerProperty(string $caller): PropertyGenerator
{
$property = new PropertyGenerator('caller');

$property->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE);
$property->omitDefaultValue(true);
$property->setDocBlock((new DocBlockGenerator())
->setWordWrap(false)
->setTags([
[
'name' => 'var',
'description' => $caller,
],
]));

return $property;
}

/**
* @param non-empty-string $fqcn
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ public function assemble(ContextInterface $context): bool
$methodBody = $this->generateMethodBody($class, $param, $method, $context);

$class->addMethodFromGenerator(
(new MethodGenerator($phpMethodName))
->setParameters($param === null ? [] : [$param])
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC)
->setBody($methodBody)
->setReturnType($this->decideOnReturnType($context, true))
->setDocBlock($docblock)
$this->generateMethod($phpMethodName, $param, $methodBody, $context, $docblock)
);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
Expand All @@ -68,6 +63,24 @@ public function assemble(ContextInterface $context): bool
return true;
}

private function generateMethod(
string $phpMethodName,
?ParameterGenerator $param,
string $methodBody,
ClientMethodContext $context,
DocBlockGenerator $docblock
): MethodGenerator {
$method = new MethodGenerator($phpMethodName);

$method->setParameters($param === null ? [] : [$param]);
$method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$method->setBody($methodBody);
$method->setReturnType($this->decideOnReturnType($context, true));
$method->setDocBlock($docblock);

return $method;
}

private function generateMethodBody(
ClassGenerator $class,
?ParameterGenerator $param,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ public function assemble(ContextInterface $context)
private function assembleConstructor(Type $type): MethodGenerator
{
$body = [];
$constructor = (new MethodGenerator('__construct'))
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$constructor = (new MethodGenerator('__construct'));

$constructor->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);

$docblock = (new DocBlockGenerator())
->setWordWrap(false)
Expand Down
65 changes: 37 additions & 28 deletions src/Phpro/SoapClient/CodeGenerator/Assembler/IteratorAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,7 @@ private function implementGetIterator(ClassGenerator $class, Property $firstProp

$methodName = 'getIterator';
$class->removeMethod($methodName);
$class->addMethodFromGenerator(
(new MethodGenerator($methodName))
->setParameters([])
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC)
->setBody(sprintf(
'return new \\ArrayIterator($this->%1$s);',
$firstProperty->getName()
))
->setReturnType('ArrayIterator')
->setDocBlock(
(new DocBlockGenerator())
->setWordWrap(false)
->setTags([
[
'name' => 'return',
'description' => '\\ArrayIterator|'. $firstProperty->getType() .'[]'
],
[
'name' => 'phpstan-return',
'description' => '\\ArrayIterator'.$arrayInfo,
],
[
'name' => 'psalm-return',
'description' => '\\ArrayIterator'.$arrayInfo,
]
])
)
);
$class->addMethodFromGenerator($this->generateGetIteratorMethod($methodName, $firstProperty, $arrayInfo));

$class->setDocBlock(
(new DocBlockGenerator())
Expand All @@ -110,4 +83,40 @@ private function implementGetIterator(ClassGenerator $class, Property $firstProp
])
);
}

private function generateGetIteratorMethod(
string $methodName,
Property $firstProperty,
string $arrayInfo
): MethodGenerator {
$method = new MethodGenerator($methodName);

$method->setParameters([]);
$method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$method->setBody(sprintf(
'return new \\ArrayIterator($this->%1$s);',
$firstProperty->getName()
));
$method->setReturnType('ArrayIterator');
$method->setDocBlock(
(new DocBlockGenerator())
->setWordWrap(false)
->setTags([
[
'name' => 'return',
'description' => '\\ArrayIterator|' . $firstProperty->getType() . '[]'
],
[
'name' => 'phpstan-return',
'description' => '\\ArrayIterator' . $arrayInfo,
],
[
'name' => 'psalm-return',
'description' => '\\ArrayIterator' . $arrayInfo,
]
])
);

return $method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,19 @@ private function implementJsonSerialize(Type $type, ClassGenerator $class)
{
$methodName = 'jsonSerialize';
$class->removeMethod($methodName);
$class->addMethodFromGenerator(
(new MethodGenerator($methodName))
->setParameters([])
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC)
->setBody($this->generateJsonSerializeBody($type, $class))
->setReturnType('array')
);
$class->addMethodFromGenerator($this->generateJsonSerializeMethod($methodName, $type, $class));
}

private function generateJsonSerializeMethod(string $methodName, Type $type, ClassGenerator $class): MethodGenerator
{
$method = new MethodGenerator($methodName);

$method->setParameters([]);
$method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$method->setBody($this->generateJsonSerializeBody($type, $class));
$method->setReturnType('array');

return $method;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ public function assemble(ContextInterface $context)
$class->removeProperty($property->getName());
}

$propertyGenerator = (new PropertyGenerator($property->getName()))
->setVisibility($this->options->visibility())
->omitDefaultValue(
!$this->options->useOptionalValue() && !(new IsConsideredNullableType())($property->getMeta())
);
$propertyGenerator = new PropertyGenerator($property->getName());

$propertyGenerator->setVisibility($this->options->visibility());
$propertyGenerator->omitDefaultValue(
!$this->options->useOptionalValue() && !(new IsConsideredNullableType())($property->getMeta())
);

if ($this->options->useDocBlocks()) {
$propertyGenerator->setDocBlock(
Expand All @@ -76,7 +77,7 @@ public function assemble(ContextInterface $context)
);
}

if ($this->options->useTypeHints()) {
if ($this->options->useTypeHints() && method_exists($propertyGenerator, 'setType')) {
$propertyGenerator->setType(TypeGenerator::fromTypeString($property->getPhpType()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,29 @@ private function implementGetResult(ContextInterface $context, ClassGenerator $c

$methodName = 'getResult';
$class->removeMethod($methodName);
$class->addMethodFromGenerator(
(new MethodGenerator($methodName))
->setParameters([])
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC)
->setReturnType(ResultInterface::class)
->setBody($this->generateGetResultBody($property))
->setDocBlock(
(new DocBlockGenerator())
->setWordWrap(false)
->setTags([
[
'name' => 'return',
'description' => $this->generateGetResultReturnTag($property)
]
])
)
$class->addMethodFromGenerator($this->generateGetResultMethod($methodName, $property));
}

private function generateGetResultMethod(string $methodName, Property $property): MethodGenerator
{
$method = new MethodGenerator($methodName);

$method->setParameters([]);
$method->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$method->setReturnType(ResultInterface::class);
$method->setBody($this->generateGetResultBody($property));
$method->setDocBlock(
(new DocBlockGenerator())
->setWordWrap(false)
->setTags([
[
'name' => 'return',
'description' => $this->generateGetResultReturnTag($property)
]
])
);

return $method;
}

/**
Expand Down
34 changes: 20 additions & 14 deletions src/Phpro/SoapClient/CodeGenerator/ClientFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,29 @@ public function generate(FileGenerator $file, $context): string
$class->addUse(EventDispatchingCaller::class);
$class->addUse(EngineCaller::class);
$class->addUse(EncoderRegistry::class);
$class->addMethodFromGenerator(
(new MethodGenerator('factory'))
->setStatic(true)
->setBody(sprintf(self::BODY, $context->getClientName(), $context->getClassmapName()))
->setReturnType($context->getClientFqcn())
->setParameter(new ParameterGenerator('wsdl', 'string'))
->setDocBlock(
(new DocBlockGenerator())
->setShortDescription(
'This factory can be used as a starting point '.
'to create your own specialized factory. Feel free to modify.'
)
)
);
$class->addMethodFromGenerator($this->generateFactoryMethod($context));

$file->setClass($class);

return $file->generate();
}

private function generateFactoryMethod(ClientFactoryContext $context): MethodGenerator
{
$method = new MethodGenerator('factory');

$method->setStatic(true);
$method->setBody(sprintf(self::BODY, $context->getClientName(), $context->getClassmapName()));
$method->setReturnType($context->getClientFqcn());
$method->setParameter(new ParameterGenerator('wsdl', 'string'));
$method->setDocBlock(
(new DocBlockGenerator())
->setShortDescription(
'This factory can be used as a starting point ' .
'to create your own specialized factory. Feel free to modify.'
)
);

return $method;
}
}
Loading

0 comments on commit 4a64771

Please sign in to comment.