Skip to content

Commit

Permalink
Rework code generator models for easier 3rd-party class type encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Oct 22, 2024
1 parent 8946e9c commit ab0c9f3
Show file tree
Hide file tree
Showing 38 changed files with 130 additions and 96 deletions.
2 changes: 1 addition & 1 deletion spec/Phpro/SoapClient/CodeGenerator/Model/TypeMapSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TypeMapSpec extends ObjectBehavior
function let()
{
$this->beConstructedWith($namespace = 'MyNamespace', [
new Type($namespace, 'type1', [
new Type($namespace, 'type1', 'type1', [
new Property('prop1', 'string', $namespace, XsdType::create('string'))
], XsdType::create('MyType'))
]);
Expand Down
5 changes: 4 additions & 1 deletion spec/Phpro/SoapClient/CodeGenerator/Model/TypeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Phpro\SoapClient\CodeGenerator\Model\Property;
use Phpro\SoapClient\CodeGenerator\Model\Type;
use Phpro\SoapClient\CodeGenerator\Util\Normalizer;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Soap\Engine\Metadata\Model\TypeMeta;
Expand All @@ -22,6 +23,7 @@ function let()
$this->beConstructedWith(
$namespace = 'MyNamespace',
'myType',
'MyType',
[new Property('prop1', 'string', $namespace, XsdType::create('string'))],
XsdType::create('MyType')
);
Expand Down Expand Up @@ -58,7 +60,7 @@ function it_has_meta()

function it_should_not_replace_underscores_in_paths()
{
$this->beConstructedWith('MyNamespace', 'my_type_3_2', ['prop1' => 'string'], XsdType::create('MyType'));
$this->beConstructedWith('MyNamespace', 'my_type_3_2', Normalizer::normalizeClassname('my_type_3_2'), ['prop1' => 'string'], XsdType::create('MyType'));
$this->getFileInfo('my/some_dir')->getPathname()->shouldReturn('my/some_dir/MyType32.php');
}

Expand All @@ -67,6 +69,7 @@ function it_should_prefix_reserved_keywords()
$this->beConstructedWith(
$namespace = 'MyNamespace',
'Final',
Normalizer::normalizeClassname('Final'),
[new Property('xor', 'string', $namespace, XsdType::create('string'))],
XsdType::create('MyType')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,28 @@ function it_can_not_apply_to_regular_context(ContextInterface $context)

function it_can_apply_to_type_context(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'MyAbstract', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'MyAbstract', 'MyAbstract', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_apply_to_property_context(RuleInterface $subRule, PropertyContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'MyAbstract', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'MyAbstract', 'MyAbstract', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_not_apply_on_invalid_type(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'NotAbstract', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'NotAbstract', 'NotAbstract', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(false);
}

function it_can_apply_if_subrule_does_not_apply(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'MyAbstract', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'MyAbstract', 'MyAbstract', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(false);
$this->appliesToContext($context)->shouldReturn(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ function it_can_not_apply_to_regular_context(ContextInterface $context)

function it_can_apply_to_type_context(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'MyExtending', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'MyExtending', 'MyExtending', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_apply_to_property_context(RuleInterface $subRule, PropertyContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'MyExtending', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'MyExtending', 'MyExtending', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_not_apply_on_invalid_type(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'NotExtending', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'NotExtending', 'NotExtending', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(false);
}

function it_can_apply_if_subrule_does_not_apply(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'MyExtending', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'MyExtending', 'MyExtending', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(false);
$this->appliesToContext($context)->shouldReturn(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ function it_can_not_apply_to_regular_context(ContextInterface $context)

function it_can_apply_to_type_context(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'RequestType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'RequestType', 'RequestType', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_apply_to_property_context(RuleInterface $subRule, PropertyContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'RequestType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'RequestType', 'RequestType', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_not_apply_on_invalid_type(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'InvalidTypeName', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'InvalidTypeName', 'InvalidTypeName', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(false);
}

function it_can_apply_if_subrule_does_not_apply(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'RequestType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'RequestType', 'RequestType', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(false);
$this->appliesToContext($context)->shouldReturn(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ function it_can_not_apply_to_regular_context(ContextInterface $context)

function it_can_apply_to_type_context(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'ResultType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'ResultType', 'ResultType', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_apply_to_property_context(RuleInterface $subRule, PropertyContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'ResultType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'ResultType', 'ResultType', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_not_apply_on_invalid_type(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'InvalidTypeName', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'InvalidTypeName', 'InvalidTypeName', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(false);
}

function it_can_apply_if_subrule_does_not_apply(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'ResultType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'ResultType', 'ResultType', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(false);
$this->appliesToContext($context)->shouldReturn(false);
}
Expand Down
14 changes: 7 additions & 7 deletions spec/Phpro/SoapClient/CodeGenerator/Rules/TypeMapRuleSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,48 @@ function it_can_not_apply_to_regular_context(ContextInterface $context)

function it_can_apply_to_type_context(RuleInterface $rule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', 'SomeType', [], XsdType::create('MyType')));
$rule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_apply_to_property_context(RuleInterface $rule, PropertyContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', 'SomeType', [], XsdType::create('MyType')));
$rule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_apply_the_default_assembler_to_unknown_types(RuleInterface $defaultRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'UnknownType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'UnknownType', 'UnknownType', [], XsdType::create('MyType')));
$defaultRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_not_apply_to_knwon_types_with_no_rule(TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'NullType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'NullType', 'NullType', [], XsdType::create('MyType')));
$this->appliesToContext($context)->shouldReturn(false);
}

function it_can_not_apply_if_rule_does_not_apply(RuleInterface $rule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', 'SomeType', [], XsdType::create('MyType')));
$rule->appliesToContext($context)->willReturn(false);
$this->appliesToContext($context)->shouldReturn(false);
}

function it_applies_a_specified_rule_to_known_types(RuleInterface $rule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'SomeType', 'SomeType', [], XsdType::create('MyType')));
$rule->apply($context)->shouldBeCalled();
$this->apply($context);
}

function it_applies_the_default_rule_to_unknown_types(RuleInterface $defaultRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'UnknownType', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'UnknownType', 'UnknownType', [], XsdType::create('MyType')));
$defaultRule->apply($context)->shouldBeCalled();
$this->apply($context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ function it_can_not_apply_to_regular_context(ContextInterface $context)

function it_can_apply_to_type_context(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'TypeName', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'TypeName', 'TypeName', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_apply_to_property_context( RuleInterface $subRule, PropertyContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'TypeName', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'TypeName', 'TypeName', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(true);
}

function it_can_not_apply_on_invalid_regex(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'InvalidTypeName', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'InvalidTypeName', 'InvalidTypeName', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(true);
$this->appliesToContext($context)->shouldReturn(false);
}

function it_can_apply_if_subrule_does_not_apply(RuleInterface $subRule, TypeContext $context)
{
$context->getType()->willReturn(new Type('MyNamespace', 'TypeName', [], XsdType::create('MyType')));
$context->getType()->willReturn(new Type('MyNamespace', 'TypeName', 'TypeName', [], XsdType::create('MyType')));
$subRule->appliesToContext($context)->willReturn(false);
$this->appliesToContext($context)->shouldReturn(false);
}
Expand Down
2 changes: 2 additions & 0 deletions spec/Phpro/SoapClient/CodeGenerator/TypeGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function it_generates_types(RuleSetInterface $ruleSet, FileGenerator $file, Clas
$type = new Type(
$namespace = 'MyNamespace',
'MyType',
'MyType',
[new Property('prop1', 'string', $namespace, XsdType::create('string'))],
XsdType::create('MyType')
);
Expand All @@ -72,6 +73,7 @@ function it_generates_types_for_file_without_classes(RuleSetInterface $ruleSet,
$type = new Type(
$namespace = 'MyNamespace',
'MyType',
'MyType',
[new Property('prop1', 'string', $namespace, XsdType::create('string'))],
XsdType::create('MyType')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Laminas\Code\Generator\PropertyGenerator;
use Soap\Engine\Metadata\Model\TypeMeta;
use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType;
use function VeeWee\Reflecta\Reflect\property_get;

/**
* Class PropertyAssembler
Expand Down Expand Up @@ -77,6 +78,11 @@ public function assemble(ContextInterface $context)
}

if ($this->options->useTypeHints()) {
if ($property->getName() === 'version') {
//dd($property);
//dd(property_get($property, 'type'), $property->getType(), $property->getPhpType());
}

$propertyGenerator->setType(TypeGenerator::fromTypeString($property->getPhpType()));
}

Expand Down
5 changes: 4 additions & 1 deletion src/Phpro/SoapClient/CodeGenerator/Model/ClientMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phpro\SoapClient\CodeGenerator\Model;

use Phpro\SoapClient\CodeGenerator\Util\Normalizer;
use Soap\Engine\Metadata\Model\Method as MetadataMethod;
use Soap\Engine\Metadata\Model\MethodMeta;
use Soap\Engine\Metadata\Model\Parameter as MetadataParameter;
Expand Down Expand Up @@ -35,6 +36,8 @@ class ClientMethod
private MethodMeta $meta;

/**
* @internal - Use ClientMethod::fromMetadata instead
*
* TypeModel constructor.
*
* @param non-empty-string $name
Expand Down Expand Up @@ -71,7 +74,7 @@ function (MetadataParameter $parameter) use ($parameterNamespace) {
iterator_to_array($method->getParameters())
),
ReturnType::fromMetaData($parameterNamespace, $method->getReturnType()),
$parameterNamespace,
Normalizer::normalizeNamespace($parameterNamespace),
$method->getMeta()
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Phpro/SoapClient/CodeGenerator/Model/ClientMethodMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ClientMethodMap
private $methods;

/**
* @internal - Use ClientMethodMap::fromMetadata instead
*
* ClientMethodMap constructor.
*
* @param array|ClientMethod[] $methods
Expand Down
14 changes: 8 additions & 6 deletions src/Phpro/SoapClient/CodeGenerator/Model/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Parameter
private TypeMeta $meta;

/**
* @internal - Use Parameter::fromMetadata instead
*
* Parameter constructor.
*
* @param non-empty-string $name
Expand All @@ -39,9 +41,9 @@ class Parameter
*/
public function __construct(string $name, string $type, string $namespace, XsdType $xsdType)
{
$this->name = Normalizer::normalizeProperty($name);
$this->type = Normalizer::normalizeDataType($type);
$this->namespace = Normalizer::normalizeNamespace($namespace);
$this->name = $name;
$this->type = $type;
$this->namespace = $namespace;
$this->xsdType = $xsdType;
$this->meta = $xsdType->getMeta();
}
Expand All @@ -55,9 +57,9 @@ public static function fromMetadata(string $parameterNamespace, MetadataParamete
$typeName = (new TypeNameCalculator())($type);

return new self(
non_empty_string()->assert($parameter->getName()),
non_empty_string()->assert($typeName),
$parameterNamespace,
Normalizer::normalizeProperty(non_empty_string()->assert($parameter->getName())),
Normalizer::normalizeDataType(non_empty_string()->assert($typeName)),
Normalizer::normalizeNamespace($parameterNamespace),
$type
);
}
Expand Down
Loading

0 comments on commit ab0c9f3

Please sign in to comment.