Skip to content

Commit

Permalink
Merge pull request #528 from veewee/fix-deprecated-code-gen
Browse files Browse the repository at this point in the history
Fix code generation deprecations
  • Loading branch information
veewee authored Jun 18, 2024
2 parents ebedefd + f304d8f commit 59a5c10
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 310 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.8.0",
"laminas/laminas-code": "^4.14.0",
"php-soap/cached-engine": "~0.2",
"php-soap/engine": "^2.10.1",
"php-soap/encoding": "~0.4",
Expand Down
10 changes: 0 additions & 10 deletions spec/Phpro/SoapClient/CodeGenerator/Model/ParameterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ function is_has_a_namespace()
$this->getNamespace()->shouldBe('MyParameterType');
}

function it_returns_an_array()
{
$this->toArray()->shouldBe(
[
'name' => 'MyParameter',
'type' => '\\MyNamespace\\MyParameterType',
]
);
}

public function it_has_type_meta(): void
{
$this->getMeta()->shouldBeLike(new TypeMeta());
Expand Down
18 changes: 5 additions & 13 deletions src/Phpro/SoapClient/CodeGenerator/Assembler/ClassMapAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public function canAssemble(ContextInterface $context): bool
*/
public function assemble(ContextInterface $context)
{
$class = ClassGenerator::fromArray(
[
'name' => $context->getName(),
]
);
$class = new ClassGenerator($context->getName());
$file = $context->getFile();
$file->setClass($class);
$file->setNamespace($context->getNamespace());
Expand All @@ -55,14 +51,10 @@ public function assemble(ContextInterface $context)
$classMap = $this->assembleClassMap($typeMap, $linefeed, $file->getIndentation());
$code = $this->assembleClassMapCollection($classMap, $linefeed).$linefeed;
$class->addMethodFromGenerator(
MethodGenerator::fromArray(
[
'name' => 'getCollection',
'static' => true,
'body' => 'return '.$code,
'returntype' => ClassMapCollection::class,
]
)
(new MethodGenerator('getCollection'))
->setStatic(true)
->setBody('return '.$code)
->setReturnType(ClassMapCollection::class)
);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Phpro\SoapClient\CodeGenerator\Assembler;

use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Generator\PropertyGenerator;
Expand Down Expand Up @@ -33,36 +34,23 @@ public function assemble(ContextInterface $context)
try {
$caller = $this->generateClassNameAndAddImport(Caller::class, $class);
$class->addPropertyFromGenerator(
PropertyGenerator::fromArray([
'name' => 'caller',
'visibility' => PropertyGenerator::VISIBILITY_PRIVATE,
'omitdefaultvalue' => true,
'docblock' => DocBlockGeneratorFactory::fromArray([
'tags' => [
(new PropertyGenerator('caller'))
->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE)
->omitDefaultValue(true)
->setDocBlock((new DocBlockGenerator())
->setWordWrap(false)
->setTags([
[
'name' => 'var',
'description' => $caller,
],
]
])
])
])),
);
$class->addMethodFromGenerator(
MethodGenerator::fromArray(
[
'name' => '__construct',
'parameters' => [
ParameterGenerator::fromArray(
[
'name' => 'caller',
'type' => Caller::class,
]
)
],
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
'body' => '$this->caller = $caller;',
]
)
(new MethodGenerator('__construct'))
->setParameter(new ParameterGenerator('caller', Caller::class))
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC)
->setBody('$this->caller = $caller;')
);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
Expand Down
124 changes: 56 additions & 68 deletions src/Phpro/SoapClient/CodeGenerator/Assembler/ClientMethodAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Phpro\SoapClient\CodeGenerator\Context\ClientMethodContext;
use Phpro\SoapClient\CodeGenerator\Context\ContextInterface;
use Phpro\SoapClient\CodeGenerator\GeneratorInterface;
use Phpro\SoapClient\CodeGenerator\LaminasCodeFactory\DocBlockGeneratorFactory;
use Phpro\SoapClient\CodeGenerator\Model\ClientMethod;
use Phpro\SoapClient\CodeGenerator\Util\Normalizer;
use Phpro\SoapClient\Exception\AssemblerException;
Expand Down Expand Up @@ -55,16 +54,12 @@ public function assemble(ContextInterface $context): bool
$methodBody = $this->generateMethodBody($class, $param, $method, $context);

$class->addMethodFromGenerator(
MethodGenerator::fromArray(
[
'name' => $phpMethodName,
'parameters' => $param === null ? [] : [$param],
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
'body' => $methodBody,
'returntype' => $this->decideOnReturnType($context, true),
'docblock' => $docblock,
]
)
(new MethodGenerator($phpMethodName))
->setParameters($param === null ? [] : [$param])
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC)
->setBody($methodBody)
->setReturnType($this->decideOnReturnType($context, true))
->setDocBlock($docblock)
);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
Expand Down Expand Up @@ -117,15 +112,10 @@ private function createParamsFromContext(ClientMethodContext $context): ?Paramet
if (!$method->shouldGenerateAsMultiArgumentsRequest()) {
$param = current($context->getMethod()->getParameters());

return ParameterGenerator::fromArray($param->toArray());
return new ParameterGenerator($param->getName(), $param->getType());
}

return ParameterGenerator::fromArray(
[
'name' => 'multiArgumentRequest',
'type' => MultiArgumentRequest::class,
]
);
return new ParameterGenerator('multiArgumentRequest', MultiArgumentRequest::class);
}

/**
Expand All @@ -141,56 +131,22 @@ private function generateMultiArgumentDocblock(ClientMethodContext $context): Do
$description[] = $parameter->getType().' $'.$parameter->getName();
}

return DocBlockGeneratorFactory::fromArray(
[
'shortdescription' => $context->getMethod()->getMeta()->docs()->unwrapOr(''),
'longdescription' => implode(GeneratorInterface::EOL, $description),
'tags' => [
[
'name' => 'param',
'description' => sprintf(
'%s $%s',
$this->generateClassNameAndAddImport(
MultiArgumentRequest::class,
$class
),
'multiArgumentRequest'
),
],
[
'name' => 'return',
'description' => sprintf(
'%s & %s',
$this->generateClassNameAndAddImport(ResultInterface::class, $class),
$this->decideOnReturnType($context, false)
),
],
[
'name' => 'throws',
'description' => $this->generateClassNameAndAddImport(
SoapException::class,
return (new DocBlockGenerator())
->setWordWrap(false)
->setShortDescription($context->getMethod()->getMeta()->docs()->unwrapOr(''))
->setLongDescription(implode(GeneratorInterface::EOL, $description))
->setTags([
[
'name' => 'param',
'description' => sprintf(
'%s $%s',
$this->generateClassNameAndAddImport(
MultiArgumentRequest::class,
$class
),
],
'multiArgumentRequest'
),
],
]
);
}

/**
* @param ClientMethodContext $context
*
* @return DocBlockGenerator
*/
private function generateSingleArgumentDocblock(ClientMethodContext $context): DocBlockGenerator
{
$method = $context->getMethod();
$class = $context->getClass();
$param = current($method->getParameters());

$data = [
'shortdescription' => $context->getMethod()->getMeta()->docs()->unwrapOr(''),
'tags' => [
[
'name' => 'return',
'description' => sprintf(
Expand All @@ -206,12 +162,42 @@ private function generateSingleArgumentDocblock(ClientMethodContext $context): D
$class
),
],
]);
}

/**
* @param ClientMethodContext $context
*
* @return DocBlockGenerator
*/
private function generateSingleArgumentDocblock(ClientMethodContext $context): DocBlockGenerator
{
$method = $context->getMethod();
$class = $context->getClass();
$param = current($method->getParameters());

$shortDescription = $context->getMethod()->getMeta()->docs()->unwrapOr('');
$tags = [
[
'name' => 'return',
'description' => sprintf(
'%s & %s',
$this->generateClassNameAndAddImport(ResultInterface::class, $class),
$this->decideOnReturnType($context, false)
),
],
[
'name' => 'throws',
'description' => $this->generateClassNameAndAddImport(
SoapException::class,
$class
),
],
];

if ($param) {
array_unshift(
$data['tags'],
$tags,
[
'name' => 'param',
'description' => sprintf(
Expand All @@ -224,8 +210,10 @@ private function generateSingleArgumentDocblock(ClientMethodContext $context): D
);
}

return DocBlockGeneratorFactory::fromArray($data)
->setWordWrap(false);
return (new DocBlockGenerator())
->setWordWrap(false)
->setShortDescription($shortDescription)
->setTags($tags);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Phpro\SoapClient\CodeGenerator\Assembler;

use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use Phpro\SoapClient\CodeGenerator\Context\ContextInterface;
use Phpro\SoapClient\CodeGenerator\Context\TypeContext;
use Phpro\SoapClient\CodeGenerator\Model\Type;
use Phpro\SoapClient\CodeGenerator\LaminasCodeFactory\DocBlockGeneratorFactory;
use Phpro\SoapClient\Exception\AssemblerException;
use Laminas\Code\Generator\MethodGenerator;

Expand Down Expand Up @@ -67,21 +68,22 @@ public function assemble(ContextInterface $context)
private function assembleConstructor(Type $type): MethodGenerator
{
$body = [];
$constructor = MethodGenerator::fromArray([
'name' => '__construct',
'visibility' => MethodGenerator::VISIBILITY_PUBLIC,
]);
$docblock = DocBlockGeneratorFactory::fromArray([
'shortdescription' => 'Constructor'
]);
$constructor = (new MethodGenerator('__construct'))
->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);

$docblock = (new DocBlockGenerator())
->setWordWrap(false)
->setShortDescription('Constructor');

foreach ($type->getProperties() as $property) {
$param = (new ParameterGenerator($property->getName()));
$body[] = sprintf('$this->%1$s = $%1$s;', $property->getName());
$withTypeHints = $this->options->useTypeHints() ? ['type' => $property->getPhpType()] : [];

$constructor->setParameter(array_merge([
'name' => $property->getName(),
], $withTypeHints));
if ($this->options->useTypeHints()) {
$param->setType($property->getPhpType());
}

$constructor->setParameter($param);

if ($this->options->useDocBlocks()) {
$docblock->setTag([
Expand Down
Loading

0 comments on commit 59a5c10

Please sign in to comment.