diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5184e1e..e5617d0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,7 @@ - - - - ./tests - - - - - - src - - - + verbose="false" +> + + + src + + + + + ./tests + + - - diff --git a/src/Php/PhpConverter.php b/src/Php/PhpConverter.php index 42a8103..d305012 100644 --- a/src/Php/PhpConverter.php +++ b/src/Php/PhpConverter.php @@ -55,7 +55,7 @@ public function __construct(NamingStrategy $namingStrategy, LoggerInterface $log private $classes = []; - public function convert(array $schemas) + public function convert(array $schemas): array { $visited = []; $this->classes = []; @@ -69,7 +69,7 @@ public function convert(array $schemas) /** * @return PHPClass[] */ - private function getTypes() + private function getTypes(): array { uasort($this->classes, function ($a, $b) { return strcmp($a['class']->getFullName(), $b['class']->getFullName()); @@ -84,7 +84,7 @@ private function getTypes() return $ret; } - private function navigate(Schema $schema, array &$visited) + private function navigate(Schema $schema, array &$visited): void { if (isset($visited[spl_object_hash($schema)])) { return; @@ -105,7 +105,7 @@ private function navigate(Schema $schema, array &$visited) } } - private function visitTypeBase(PHPClass $class, Type $type) + private function visitTypeBase(PHPClass $class, Type $type): void { $class->setAbstract($type->isAbstract()); @@ -119,26 +119,28 @@ private function visitTypeBase(PHPClass $class, Type $type) $this->visitComplexType($class, $type); } } - - /** - * Process xsd:complexType xsd:sequence xsd:element - * - * @param PHPClass $class - * @param Schema $schema - * @param Sequence $sequence - */ - private function visitSequence(PHPClass $class, Schema $schema, Sequence $sequence) - { - foreach ($sequence->getElements() as $childSequence) { - if ($childSequence instanceof Group) { - $this->visitGroup($class, $schema, $childSequence); - } else { - $property = $this->visitElement($class, $schema, $childSequence); - $class->addProperty($property); - } - } - } - + + /** + * Process xsd:complexType xsd:sequence xsd:element + * + * @param PHPClass $class + * @param Schema $schema + * @param Sequence $sequence + */ + private function visitSequence(PHPClass $class, Schema $schema, Sequence $sequence): void + { + foreach ($sequence->getElements() as $childSequence) { + if ($childSequence instanceof Group) { + $this->visitGroup($class, $schema, $childSequence); + } elseif ($childSequence instanceof Choice) { + $this->visitChoice($class, $schema, $childSequence); + } else { + $property = $this->visitElement($class, $schema, $childSequence); + $class->addProperty($property); + } + } + } + /** * Process xsd:complexType xsd:choice xsd:element * @@ -146,19 +148,19 @@ private function visitSequence(PHPClass $class, Schema $schema, Sequence $sequen * @param Schema $schema * @param Choice $choice */ - private function visitChoice(PHPClass $class, Schema $schema, Choice $choice) + private function visitChoice(PHPClass $class, Schema $schema, Choice $choice): void { foreach ($choice->getElements() as $choiceOption) { - if ($choiceOption instanceof Sequence) { - $this->visitSequence($class, $schema, $choiceOption); - } else { - $property = $this->visitElement($class, $schema, $choiceOption); - $class->addProperty($property); - } + if ($choiceOption instanceof Sequence) { + $this->visitSequence($class, $schema, $choiceOption); + } else { + $property = $this->visitElement($class, $schema, $choiceOption); + $class->addProperty($property); + } } } - - private function visitGroup(PHPClass $class, Schema $schema, Group $group) + + private function visitGroup(PHPClass $class, Schema $schema, Group $group): void { foreach ($group->getElements() as $childGroup) { if ($childGroup instanceof Group) { @@ -170,7 +172,7 @@ private function visitGroup(PHPClass $class, Schema $schema, Group $group) } } - private function visitAttributeGroup(PHPClass $class, Schema $schema, AttributeGroup $att) + private function visitAttributeGroup(PHPClass $class, Schema $schema, AttributeGroup $att): void { foreach ($att->getAttributes() as $childAttr) { if ($childAttr instanceof AttributeGroup) { @@ -223,7 +225,7 @@ public function visitElementDef(ElementDef $element) return $this->classes[spl_object_hash($element)]['class']; } - public function isSkip($class) + public function isSkip($class): bool { return !empty($this->skipByType[spl_object_hash($class)]); } @@ -238,12 +240,12 @@ private function findPHPName(Type $type) substr($className, $pos + 1), substr($className, 0, $pos), ]; - } else { - return [ - $className, - null, - ]; } + + return [ + $className, + null, + ]; } $name = $this->getNamingStrategy()->getTypeName($type); @@ -318,7 +320,7 @@ public function visitType(Type $type, $force = false) /** * @param string $name * - * @return \GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPClass + * @return PHPClass */ private function visitTypeAnonymous(Type $type, $name, PHPClass $parentClass) { @@ -340,13 +342,13 @@ private function visitTypeAnonymous(Type $type, $name, PHPClass $parentClass) return $this->classes[spl_object_hash($type)]['class']; } - private function visitComplexType(PHPClass $class, ComplexType $type) + private function visitComplexType(PHPClass $class, ComplexType $type): void { $schema = $type->getSchema(); foreach ($type->getElements() as $element) { - if ($element instanceof Sequence) { - $this->visitSequence($class, $schema, $element); - } elseif ($element instanceof Choice) { + if ($element instanceof Sequence) { + $this->visitSequence($class, $schema, $element); + } elseif ($element instanceof Choice) { $this->visitChoice($class, $schema, $element); } elseif ($element instanceof Group) { $this->visitGroup($class, $schema, $element); @@ -357,7 +359,7 @@ private function visitComplexType(PHPClass $class, ComplexType $type) } } - private function visitSimpleType(PHPClass $class, SimpleType $type) + private function visitSimpleType(PHPClass $class, SimpleType $type): void { if ($restriction = $type->getRestriction()) { $parent = $restriction->getBase(); @@ -387,7 +389,7 @@ private function visitSimpleType(PHPClass $class, SimpleType $type) } } - private function handleClassExtension(PHPClass $class, Type $type) + private function handleClassExtension(PHPClass $class, Type $type): void { if ($alias = $this->getTypeAlias($type)) { $c = PHPClass::createFromFQCN($alias); @@ -401,7 +403,7 @@ private function handleClassExtension(PHPClass $class, Type $type) } } - private function visitBaseComplexType(PHPClass $class, BaseComplexType $type) + private function visitBaseComplexType(PHPClass $class, BaseComplexType $type): void { $parent = $type->getParent(); if ($parent) { @@ -449,7 +451,7 @@ private function visitAttribute(PHPClass $class, Schema $schema, AttributeItem $ * @param Element $element * @param bool $arrayize * - * @return \GoetasWebservices\Xsd\XsdToPhp\Php\Structure\PHPProperty + * @return PHPProperty */ private function visitElement(PHPClass $class, Schema $schema, ElementSingle $element, $arrayize = true) { @@ -484,7 +486,9 @@ private function visitElement(PHPClass $class, Schema $schema, ElementSingle $el $property->setType(new PHPClassOf($arg)); return $property; - } elseif ($itemOfArray = $this->isArrayNestedElement($t)) { + } + + if ($itemOfArray = $this->isArrayNestedElement($t)) { if (!$t->getName()) { if ($element instanceof ElementRef) { @@ -502,7 +506,9 @@ private function visitElement(PHPClass $class, Schema $schema, ElementSingle $el $property->setType(new PHPClassOf($elementProp)); return $property; - } elseif ($this->isArrayElement($element)) { + } + + if ($this->isArrayElement($element)) { $arg = new PHPArg($this->getNamingStrategy()->getPropertyName($element)); $arg->setType($this->findPHPElementClassName($class, $element)); @@ -528,9 +534,9 @@ private function findPHPClass(PHPClass $class, Item $node, $force = false) } if (!$node->getType()->getName()) { return $this->visitTypeAnonymous($node->getType(), $node->getName(), $class); - } else { - return $this->visitType($node->getType(), $force); } + + return $this->visitType($node->getType(), $force); } private function typeHasValue(Type $type, PHPClass $parentClass, $name) diff --git a/tests/Issues/I138/I138Test.php b/tests/Issues/I138/I138Test.php index 5ac8e14..d3516d6 100644 --- a/tests/Issues/I138/I138Test.php +++ b/tests/Issues/I138/I138Test.php @@ -8,7 +8,7 @@ class I138Test extends TestCase { - public function testChioce() + public function testChoice() { $reader = new SchemaReader(); $schema = $reader->readFile(__DIR__ . '/data.xsd'); diff --git a/tests/Issues/I168/I168Test.php b/tests/Issues/I168/I168Test.php new file mode 100644 index 0000000..f79977c --- /dev/null +++ b/tests/Issues/I168/I168Test.php @@ -0,0 +1,26 @@ +readFile(__DIR__ . '/data.xsd'); + + $phpConv = new PhpConverter(new ShortNamingStrategy()); + $phpConv->addNamespace('http://www.example.com/', 'Epa'); + + $items = $phpConv->convert([$schema]); + + $this->assertTrue($items['Epa\ComplexType']->getProperty('key')->getNullable()); + $this->assertTrue($items['Epa\ComplexType']->getProperty('stream')->getNullable()); + $this->assertTrue($items['Epa\ComplexType']->getProperty('packet')->getNullable()); + } +} \ No newline at end of file diff --git a/tests/Issues/I168/data.xsd b/tests/Issues/I168/data.xsd new file mode 100644 index 0000000..ae77d14 --- /dev/null +++ b/tests/Issues/I168/data.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + +