Commit 06d97e31 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

CQ.

No related merge requests found
Showing with 34 additions and 17 deletions
+34 -17
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace Irstea\NgModelGeneratorBundle; namespace Irstea\NgModelGeneratorBundle;
use ApiPlatform\Core\Documentation\Documentation; use ApiPlatform\Core\Documentation\Documentation;
use Assert\Assertion;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException; use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Exceptions\EmptyModelException; use Irstea\NgModelGeneratorBundle\Exceptions\EmptyModelException;
use Irstea\NgModelGeneratorBundle\Exceptions\Exception; use Irstea\NgModelGeneratorBundle\Exceptions\Exception;
...@@ -62,9 +63,8 @@ final class ModelGenerator ...@@ -62,9 +63,8 @@ final class ModelGenerator
* Serializer constructor. * Serializer constructor.
* *
* @param MetadataFactoryInterface $metadataFactory * @param MetadataFactoryInterface $metadataFactory
* @param ContextFactoryInterface $contextFactory * @param ContextFactoryInterface $contextFactory
* @param TypeFactoryInterface $typeFactory * @param Environment $twigEnv
* @param Environment $twigEnv
*/ */
public function __construct( public function __construct(
MetadataFactoryInterface $metadataFactory, MetadataFactoryInterface $metadataFactory,
...@@ -177,7 +177,8 @@ final class ModelGenerator ...@@ -177,7 +177,8 @@ final class ModelGenerator
{ {
$classNames = iterator_to_array($this->documentation->getResourceNameCollection()->getIterator()); $classNames = iterator_to_array($this->documentation->getResourceNameCollection()->getIterator());
$classes = array_map([PHPClass::class, 'get'], $classNames); $classes = array_map([PHPClass::class, 'get'], $classNames);
usort($classes, [PHPClass::class, 'baseNameOrdering']); $sorter = \Closure::fromCallable([PHPClass::class, 'baseNameOrdering']);
usort($classes, $sorter);
foreach ($classes as $class) { foreach ($classes as $class) {
yield $class => $this->metadataFactory->getResourceMetadata($class); yield $class => $this->metadataFactory->getResourceMetadata($class);
...@@ -247,6 +248,7 @@ final class ModelGenerator ...@@ -247,6 +248,7 @@ final class ModelGenerator
} }
$identifier = array_shift($identifiers); $identifier = array_shift($identifiers);
Assertion::notNull($identifier);
$operations = []; $operations = [];
$pathParser = new PathParser($properties); $pathParser = new PathParser($properties);
......
...@@ -21,8 +21,10 @@ ...@@ -21,8 +21,10 @@
namespace Irstea\NgModelGeneratorBundle\Models; namespace Irstea\NgModelGeneratorBundle\Models;
use Assert\Assertion;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException; use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Metadata\PropertyMetadata; use Irstea\NgModelGeneratorBundle\Metadata\PropertyMetadata;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\Property;
/** /**
* Class ClassInfo. * Class ClassInfo.
...@@ -315,6 +317,7 @@ final class ClassInfo implements ClassName ...@@ -315,6 +317,7 @@ final class ClassInfo implements ClassName
return; return;
} }
/** @var PropertyMetadata[][] $classProperties */
$classProperties = []; $classProperties = [];
if (!$this->abstract) { if (!$this->abstract) {
$classProperties[] = $this->virtualProperties; $classProperties[] = $this->virtualProperties;
...@@ -324,6 +327,7 @@ final class ClassInfo implements ClassName ...@@ -324,6 +327,7 @@ final class ClassInfo implements ClassName
$classProperties[] = $child->virtualProperties; $classProperties[] = $child->virtualProperties;
} }
/** @var PropertyMetadata[] $commonProperties */
switch (\count($classProperties)) { switch (\count($classProperties)) {
case 0: case 0:
return; return;
...@@ -338,7 +342,10 @@ final class ClassInfo implements ClassName ...@@ -338,7 +342,10 @@ final class ClassInfo implements ClassName
return; return;
} }
$this->virtualProperties = array_replace($this->virtualProperties, $commonProperties); $result = array_replace($this->virtualProperties, $commonProperties);
Assertion::notNull($result);
$this->virtualProperties = $result;
} }
/** /**
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types\Objects; namespace Irstea\NgModelGeneratorBundle\Models\Types\Objects;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException; use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Models\PHPClass;
use Irstea\NgModelGeneratorBundle\Models\Types\AbstractType; use Irstea\NgModelGeneratorBundle\Models\Types\AbstractType;
use Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType; use Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType;
use Irstea\NgModelGeneratorBundle\TypescriptHelper; use Irstea\NgModelGeneratorBundle\TypescriptHelper;
...@@ -50,7 +51,9 @@ class AnonymousObject extends AbstractType ...@@ -50,7 +51,9 @@ class AnonymousObject extends AbstractType
} }
$this->properties[$name] = $property; $this->properties[$name] = $property;
} }
uasort($this->properties, [Property::class, 'compare']);
$sorter = \Closure::fromCallable([Property::class, 'compare']);
uasort($this->properties, $sorter);
} }
/** /**
...@@ -110,13 +113,19 @@ class AnonymousObject extends AbstractType ...@@ -110,13 +113,19 @@ class AnonymousObject extends AbstractType
/** @var Property $property */ /** @var Property $property */
foreach ($this->properties as $property) { foreach ($this->properties as $property) {
$tpl = $property->isNullable() ? '(!(%s) || %s)' : '(%s && %s)'; if ($property->isNullable()) {
$tests[] = sprintf( $tests[] = sprintf(
$tpl, '(!(%s) || %s)',
TypescriptHelper::propertyTestor($expr, $property->getName()), TypescriptHelper::propertyTestor($expr, $property->getName()),
$expr, $property->getType()->checkType($property->getUsage($expr), false)
$property->getType()->checkType($property->getUsage($expr), false) );
); } else {
$tests[] = sprintf(
'(%s && %s)',
TypescriptHelper::propertyTestor($expr, $property->getName()),
$property->getType()->checkType($property->getUsage($expr), false)
);
}
} }
return '(' . implode("\n && ", $tests) . ')'; return '(' . implode("\n && ", $tests) . ')';
......
...@@ -64,11 +64,10 @@ final class OperationMapper ...@@ -64,11 +64,10 @@ final class OperationMapper
/** /**
* OperationMapper constructor. * OperationMapper constructor.
* *
* @param TypeFactoryInterface $typeFactory
* @param ContextFactoryInterface $contextFactory * @param ContextFactoryInterface $contextFactory
* @param PathParserInterface $pathParser * @param PathParserInterface $pathParser
* @param OperationMetadata $operation * @param OperationMetadata $operation
* @param Path|null $iri * @param Path|null $iri
*/ */
public function __construct( public function __construct(
ContextFactoryInterface $contextFactory, ContextFactoryInterface $contextFactory,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment