Skip to content

Commit

Permalink
chore: Bumped
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Jul 5, 2024
1 parent b55b6a1 commit 0f2fa77
Show file tree
Hide file tree
Showing 22 changed files with 553 additions and 522 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ jobs:
run: vendor/bin/phpcs --extensions=php --warning-severity=0 --standard=PSR12 -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
- name: Run unit tests
run: vendor/bin/phpunit -v --whitelist ./src --coverage-clover ./build/logs/clover.xml src/Test
- name: Run atoum unit tests
run: vendor/bin/atoum -f tests/units/*
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2024 Ambroise Maupate
Copyright © 2023 Ambroise Maupate

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test:
vendor/bin/phpcbf --report=full --report-file=./report.txt --extensions=php --warning-severity=0 --standard=PSR12 -p ./src
vendor/bin/phpstan analyse -c phpstan.neon
vendor/bin/phpunit -v --whitelist ./src --coverage-clover ./build/logs/clover.xml src/Test
vendor/bin/atoum -f tests/units/*
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"php": ">=8.1",
"ext-json": "*",
"roadiz/nodetype-contracts": "~1.1.2",
"symfony/string": "6.4.*",
"symfony/yaml": "6.4.*",
"symfony/serializer": "6.4.*",
"symfony/options-resolver": "6.4.*"
"symfony/string": "5.4.*",
"symfony/yaml": "5.4.*",
"symfony/serializer": "5.4.*",
"symfony/options-resolver": "5.4.*"
},
"suggest": {
"api-platform/core": "If you need to create ApiFilter annotation right into your entities"
Expand All @@ -32,13 +32,13 @@
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^1.5.3",
"phpunit/phpunit": "^9.5",
"api-platform/core": "~3.2.14"
"atoum/atoum": "^4.0.0",
"api-platform/core": "~2.7.0"
},
"extra": {
"branch-alias": {
"dev-main": "2.3.x-dev",
"dev-develop": "2.4.x-dev"
"dev-main": "2.2.x-dev",
"dev-develop": "2.3.x-dev"
}
}
}
7 changes: 3 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
parameters:
level: 7
level: max
paths:
- src
excludePaths:
- */node_modules/*
- */bower_components/*
- */static/*
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.generics
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
83 changes: 22 additions & 61 deletions src/Attribute/AttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class AttributeGenerator
{
protected string $className;
/**
* @var array<string|int, string|int|array>
* @var array<string|int, string|int>
*/
protected array $parameters;

/**
* @param string $className
* @param array<string|int, string|int|array|null> $parameters
* @param int[]|string[] $parameters
*/
public function __construct(string $className, array $parameters = [])
{
Expand All @@ -24,94 +24,55 @@ public function __construct(string $className, array $parameters = [])

public static function wrapString(string $string): string
{
return sprintf('"%s"', str_replace('"', '\\"', $string));
return sprintf('"%s"', $string);
}

public function generate(int $currentIndentation = 0): string
{
$formattedParams = [];
if (count($this->parameters) > 3) {
foreach ($this->parameters as $name => $parameter) {
if (empty($parameter)) {
continue;
if (is_string($name) && !empty($name)) {
$formattedParams[] = sprintf(
'%s%s: %s',
str_repeat(' ', $currentIndentation + 4),
$name,
$parameter
);
} else {
$formattedParams[] = sprintf(
'%s%s',
str_repeat(' ', $currentIndentation + 4),
$parameter
);
}
$formattedParams[] = $this->formatProperties($name, $parameter, $currentIndentation);
}
return
str_repeat(' ', $currentIndentation) .
$this->className .
sprintf(
'(%s%s%s)',
PHP_EOL,
implode(',' . PHP_EOL, array_filter($formattedParams)),
implode(',' . PHP_EOL, $formattedParams),
PHP_EOL . str_repeat(' ', $currentIndentation),
);
} elseif (count($this->parameters) > 0) {
foreach ($this->parameters as $name => $parameter) {
if (empty($parameter)) {
continue;
if (is_string($name) && !empty($name)) {
$formattedParams[] = sprintf('%s: %s', $name, $parameter);
} else {
$formattedParams[] = $parameter;
}
$formattedParams[] = $this->formatProperties($name, $parameter, -4);
}
return
str_repeat(' ', $currentIndentation) .
$this->className .
sprintf(
'(%s)',
implode(', ', array_filter($formattedParams))
implode(', ', $formattedParams)
);
} else {
return str_repeat(' ', $currentIndentation) . $this->className;
}
}

/**
* @param string $name
* @param array<string, mixed> $parameter
* @param int $currentIndentation
* @return string
* @throws \JsonException
*/
protected function formatArrayObject(string $name, array $parameter, int $currentIndentation = 0): string
{
$encodedParameterContent = [];
foreach ($parameter as $key => $value) {
if (is_string($key)) {
$encodedParameterContent[] = sprintf(
'%s => %s',
self::wrapString($key),
\json_encode($value, \JSON_THROW_ON_ERROR)
);
}
}
return sprintf(
'%s%s: %s',
str_repeat(' ', $currentIndentation + 4),
$name,
'[' . implode(', ', $encodedParameterContent) . ']'
);
}

protected function formatProperties(string|int $name, mixed $parameter, int $currentIndentation = 0): ?string
{
if (empty($parameter)) {
return null;
}
if (is_string($name) && \is_array($parameter)) {
return $this->formatArrayObject($name, $parameter, $currentIndentation);
}
if (is_string($name) && !empty($name)) {
return sprintf(
'%s%s: %s',
str_repeat(' ', $currentIndentation + 4),
$name,
$parameter
);
}
return sprintf(
'%s%s',
str_repeat(' ', $currentIndentation + 4),
$parameter
);
}
}
9 changes: 2 additions & 7 deletions src/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,10 @@ protected function getClassAttributes(): string
*/
protected function getClassAnnotations(): string
{
$annotations = [
$this->nodeType->getName() . ' node-source entity.',
$this->nodeType->getDescription()
];
$annotations = array_filter($annotations);

return '
/**
* ' . implode(PHP_EOL . ' * ', $annotations) . '
* DO NOT EDIT
* Generated custom node-source type by Roadiz.
*/' . PHP_EOL;
}

Expand Down
21 changes: 0 additions & 21 deletions src/Field/AbstractFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,6 @@ protected function getFieldAttributes(bool $exclude = false): array
$attributes[] = new AttributeGenerator('SymfonySerializer\Groups', [
$this->getSerializationGroups()
]);

$description = $this->field->getLabel();
if (!empty($this->field->getDescription())) {
$description .= ': ' . $this->field->getDescription();
}
if ($this->field->isEnum() && null !== $defaultValues = $this->field->getDefaultValues()) {
$enumValues = explode(',', $defaultValues);
$enumValues = array_filter(array_map('trim', $enumValues));
$openapiContext = [
'type' => 'string',
'enum' => $enumValues,
'example' => $enumValues[0] ?? null,
];
}
$attributes[] = new AttributeGenerator('\ApiPlatform\Metadata\ApiProperty', [
'description' => AttributeGenerator::wrapString($description),
'schema' => $openapiContext ?? null,
'example' => $this->field->getPlaceholder() ?
AttributeGenerator::wrapString($this->field->getPlaceholder()) :
null,
]);
if ($this->getSerializationMaxDepth() > 0) {
$attributes[] = new AttributeGenerator('SymfonySerializer\MaxDepth', [
$this->getSerializationMaxDepth()
Expand Down
35 changes: 23 additions & 12 deletions src/Field/CustomFormsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ public function getFieldGetter(): string
public function ' . $this->field->getGetterName() . '(): array
{
if (null === $this->' . $this->field->getVarName() . ') {
if (null !== $this->objectManager) {
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
$this->' . $this->field->getVarName() . ' = $this->objectManager
->getRepository(' . $this->options['custom_form_class'] . '::class)
->findByNodeAndFieldName(
->findByNodeAndField(
$this->getNode(),
\'' . $this->field->getName() . '\'
$this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '")
);
} else {
$this->' . $this->field->getVarName() . ' = [];
Expand All @@ -80,15 +84,22 @@ protected function getFieldSetter(): string
*/
public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['custom_form_class'] . ' $customForm): static
{
if (null !== $this->objectManager) {
$nodeCustomForm = new ' . $this->options['custom_form_proxy_class'] . '(
$this->getNode(),
$customForm
);
$nodeCustomForm->setFieldName(\'' . $this->field->getName() . '\');
$this->objectManager->persist($nodeCustomForm);
$this->getNode()->addCustomForm($nodeCustomForm);
$this->' . $this->field->getVarName() . ' = null;
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
$field = $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '");
if (null !== $field) {
$nodeCustomForm = new ' . $this->options['custom_form_proxy_class'] . '(
$this->getNode(),
$customForm,
$field
);
$this->objectManager->persist($nodeCustomForm);
$this->getNode()->addCustomForm($nodeCustomForm);
$this->' . $this->field->getVarName() . ' = null;
}
}
return $this;
}' . PHP_EOL;
Expand Down
37 changes: 24 additions & 13 deletions src/Field/DocumentsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ public function getFieldGetter(): string
public function ' . $this->field->getGetterName() . '(): array
{
if (null === $this->' . $this->field->getVarName() . ') {
if (null !== $this->objectManager) {
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
$this->' . $this->field->getVarName() . ' = $this->objectManager
->getRepository(' . $this->options['document_class'] . '::class)
->findByNodeSourceAndFieldName(
->findByNodeSourceAndField(
$this,
\'' . $this->field->getName() . '\'
$this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '")
);
} else {
$this->' . $this->field->getVarName() . ' = [];
Expand All @@ -88,16 +92,23 @@ protected function getFieldSetter(): string
*/
public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['document_class'] . ' $document): static
{
if (null !== $this->objectManager) {
$nodeSourceDocument = new ' . $this->options['document_proxy_class'] . '(
$this,
$document
);
$nodeSourceDocument->setFieldName(\'' . $this->field->getName() . '\');
if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) {
$this->objectManager->persist($nodeSourceDocument);
$this->addDocumentsByFields($nodeSourceDocument);
$this->' . $this->field->getVarName() . ' = null;
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
$field = $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '");
if (null !== $field) {
$nodeSourceDocument = new ' . $this->options['document_proxy_class'] . '(
$this,
$document,
$field
);
if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) {
$this->objectManager->persist($nodeSourceDocument);
$this->addDocumentsByFields($nodeSourceDocument);
$this->' . $this->field->getVarName() . ' = null;
}
}
}
return $this;
Expand Down
10 changes: 7 additions & 3 deletions src/Field/NodesFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ public function getFieldGetter(): string
public function ' . $this->field->getGetterName() . 'Sources(): array
{
if (null === $this->' . $this->getFieldSourcesName() . ') {
if (null !== $this->objectManager) {
if (
null !== $this->objectManager &&
null !== $this->getNode() &&
null !== $this->getNode()->getNodeType()
) {
$this->' . $this->getFieldSourcesName() . ' = $this->objectManager
->getRepository(' . $this->getRepositoryClass() . '::class)
->findByNodesSourcesAndFieldNameAndTranslation(
->findByNodesSourcesAndFieldAndTranslation(
$this,
\'' . $this->field->getName() . '\'
$this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '")
);
} else {
$this->' . $this->getFieldSourcesName() . ' = [];
Expand Down
2 changes: 1 addition & 1 deletion src/RepositoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function getClassHeader(): string
use Doctrine\Persistence\ManagerRegistry;
use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\NodeSourceSearchHandlerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
Expand Down
Loading

0 comments on commit 0f2fa77

Please sign in to comment.