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 @@
namespace Irstea\NgModelGeneratorBundle;
use ApiPlatform\Core\Documentation\Documentation;
use Assert\Assertion;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Exceptions\EmptyModelException;
use Irstea\NgModelGeneratorBundle\Exceptions\Exception;
......@@ -62,9 +63,8 @@ final class ModelGenerator
* Serializer constructor.
*
* @param MetadataFactoryInterface $metadataFactory
* @param ContextFactoryInterface $contextFactory
* @param TypeFactoryInterface $typeFactory
* @param Environment $twigEnv
* @param ContextFactoryInterface $contextFactory
* @param Environment $twigEnv
*/
public function __construct(
MetadataFactoryInterface $metadataFactory,
......@@ -177,7 +177,8 @@ final class ModelGenerator
{
$classNames = iterator_to_array($this->documentation->getResourceNameCollection()->getIterator());
$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) {
yield $class => $this->metadataFactory->getResourceMetadata($class);
......@@ -247,6 +248,7 @@ final class ModelGenerator
}
$identifier = array_shift($identifiers);
Assertion::notNull($identifier);
$operations = [];
$pathParser = new PathParser($properties);
......
......@@ -21,8 +21,10 @@
namespace Irstea\NgModelGeneratorBundle\Models;
use Assert\Assertion;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Metadata\PropertyMetadata;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\Property;
/**
* Class ClassInfo.
......@@ -315,6 +317,7 @@ final class ClassInfo implements ClassName
return;
}
/** @var PropertyMetadata[][] $classProperties */
$classProperties = [];
if (!$this->abstract) {
$classProperties[] = $this->virtualProperties;
......@@ -324,6 +327,7 @@ final class ClassInfo implements ClassName
$classProperties[] = $child->virtualProperties;
}
/** @var PropertyMetadata[] $commonProperties */
switch (\count($classProperties)) {
case 0:
return;
......@@ -338,7 +342,10 @@ final class ClassInfo implements ClassName
return;
}
$this->virtualProperties = array_replace($this->virtualProperties, $commonProperties);
$result = array_replace($this->virtualProperties, $commonProperties);
Assertion::notNull($result);
$this->virtualProperties = $result;
}
/**
......
......@@ -22,6 +22,7 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types\Objects;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Models\PHPClass;
use Irstea\NgModelGeneratorBundle\Models\Types\AbstractType;
use Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
......@@ -50,7 +51,9 @@ class AnonymousObject extends AbstractType
}
$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
/** @var Property $property */
foreach ($this->properties as $property) {
$tpl = $property->isNullable() ? '(!(%s) || %s)' : '(%s && %s)';
$tests[] = sprintf(
$tpl,
TypescriptHelper::propertyTestor($expr, $property->getName()),
$expr,
$property->getType()->checkType($property->getUsage($expr), false)
);
if ($property->isNullable()) {
$tests[] = sprintf(
'(!(%s) || %s)',
TypescriptHelper::propertyTestor($expr, $property->getName()),
$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) . ')';
......
......@@ -64,11 +64,10 @@ final class OperationMapper
/**
* OperationMapper constructor.
*
* @param TypeFactoryInterface $typeFactory
* @param ContextFactoryInterface $contextFactory
* @param PathParserInterface $pathParser
* @param OperationMetadata $operation
* @param Path|null $iri
* @param PathParserInterface $pathParser
* @param OperationMetadata $operation
* @param Path|null $iri
*/
public function __construct(
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