Skip to content

Commit 8641a64

Browse files
committed
generate setter for simple arrays
1 parent c50ccf5 commit 8641a64

File tree

8 files changed

+68
-6
lines changed

8 files changed

+68
-6
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ parameters:
6161
author: false|string,
6262
fieldVisibility: 'private'|'protected'|'public'|null,
6363
accessorMethods: boolean,
64+
useSimpleArraySetter: boolean,
6465
fluentMutatorMethods: boolean,
6566
rangeMapping: array<string, string>,
6667
allTypes: boolean,

src/Model/Class_.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public function toNetteFile(array $config, InflectorInterface $inflector, ?PhpFi
218218
{
219219
$useDoctrineCollections = $config['doctrine']['useCollection'];
220220
$useAccessors = $config['accessorMethods'];
221+
$useSimpleArraySetter = $config['useSimpleArraySetter'];
221222
$useFluentMutators = $config['fluentMutatorMethods'];
222223
$fileHeader = $config['header'] ?? null;
223224
$fieldVisibility = $config['fieldVisibility'];
@@ -333,7 +334,7 @@ public function toNetteFile(array $config, InflectorInterface $inflector, ?PhpFi
333334
foreach ($sortedProperties as $property) {
334335
foreach ($property->generateNetteMethods(static function ($string) use ($inflector) {
335336
return $inflector->singularize($string)[0];
336-
}, $namespace, $useDoctrineCollections, $useFluentMutators) as $method) {
337+
}, $namespace, $useDoctrineCollections, $useFluentMutators, $useSimpleArraySetter) as $method) {
337338
$methods[] = $method;
338339
}
339340
}

src/Model/Property.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function isArray(): bool
8181
return $this->type instanceof ArrayType;
8282
}
8383

84+
public function isSimpleArray(): bool
85+
{
86+
return 'array' === $this->typeHint;
87+
}
88+
8489
public function addAnnotation(string $annotation): self
8590
{
8691
if ('' === $annotation || !\in_array($annotation, $this->annotations, true)) {
@@ -154,7 +159,7 @@ public function toNetteProperty(PhpNamespace $namespace, ?string $visibility = n
154159
$property->setType($this->resolveName($namespace, $this->typeHint));
155160
}
156161

157-
if (!$this->isArray() || $this->isTypeHintedAsCollection()) {
162+
if (!$this->isArray() || $this->isSimpleArray() || $this->isTypeHintedAsCollection()) {
158163
$property->setNullable($this->isNullable);
159164
}
160165

@@ -195,9 +200,10 @@ public function generateNetteMethods(
195200
PhpNamespace $namespace,
196201
bool $useDoctrineCollections = true,
197202
bool $useFluentMutators = false,
203+
bool $useSimpleArraySetter = false,
198204
): array {
199205
return array_merge(
200-
$this->generateMutators($singularize, $namespace, $useDoctrineCollections, $useFluentMutators),
206+
$this->generateMutators($singularize, $namespace, $useDoctrineCollections, $useFluentMutators, $useSimpleArraySetter),
201207
$this->isReadable ? [$this->generateGetter($namespace)] : []
202208
);
203209
}
@@ -214,7 +220,7 @@ private function generateGetter(PhpNamespace $namespace): Method
214220
}
215221
if ($this->typeHint) {
216222
$getter->setReturnType($this->resolveName($namespace, $this->typeHint));
217-
if ($this->isNullable && !$this->isArray()) {
223+
if ($this->isNullable && (!$this->isArray() || $this->isSimpleArray())) {
218224
$getter->setReturnNullable();
219225
}
220226
}
@@ -231,13 +237,14 @@ private function generateMutators(
231237
PhpNamespace $namespace,
232238
bool $useDoctrineCollections = true,
233239
bool $useFluentMutators = false,
240+
bool $useSimpleArraySetter = false,
234241
): array {
235242
if (!$this->isWritable) {
236243
return [];
237244
}
238245

239246
$mutators = [];
240-
if ($this->isArray()) {
247+
if ($this->isArray() && (!$this->isSimpleArray() || !$useSimpleArraySetter)) {
241248
$singularProperty = $singularize($this->name());
242249

243250
$adder = (new Method('add'.ucfirst($singularProperty)))->setVisibility(ClassType::VISIBILITY_PUBLIC);

src/SchemaGeneratorConfiguration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public function getConfigTreeBuilder(): TreeBuilder
167167
->scalarNode('author')->defaultFalse()->info('The value of the phpDoc\'s @author annotation')->example('Kévin Dunglas <[email protected]>')->end()
168168
->enumNode('fieldVisibility')->values(['private', 'protected', 'public'])->defaultValue('private')->cannotBeEmpty()->info('Visibility of entities fields')->end()
169169
->booleanNode('accessorMethods')->defaultTrue()->info('Set this flag to false to not generate getter, setter, adder and remover methods')->end()
170+
->booleanNode('useSimpleArraySetter')->defaultFalse()->info('Set this flag to true to generate setter method for simple arrays instead of adder and remover methods')->end()
170171
->booleanNode('fluentMutatorMethods')->defaultFalse()->info('Set this flag to true to generate fluent setter, adder and remover methods')->end()
171172
->arrayNode('rangeMapping')
172173
->useAttributeAsKey('name')

tests/Command/DumpConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ interface: App\Model # Example: App\Model
154154
# Set this flag to false to not generate getter, setter, adder and remover methods
155155
accessorMethods: true
156156
157+
# Set this flag to true to generate setter method for simple arrays instead of adder and remover methods
158+
useSimpleArraySetter: false
159+
157160
# Set this flag to true to generate fluent setter, adder and remover methods
158161
fluentMutatorMethods: false
159162
rangeMapping:

tests/Command/GenerateCommandTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,43 @@ class Page extends Object_
538538
self::assertFalse($this->fs->exists("$outputDir/App/Entity/Travel.php"));
539539
}
540540

541+
public function testGeneratedSimpleArray(): void
542+
{
543+
$outputDir = __DIR__.'/../../build/simple-array';
544+
$config = __DIR__.'/../config/simple-array.yaml';
545+
546+
$this->fs->mkdir($outputDir);
547+
548+
$commandTester = new CommandTester(new GenerateCommand());
549+
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
550+
$source = file_get_contents("$outputDir/App/Entity/Project.php");
551+
552+
$this->assertStringContainsString(<<<'PHP'
553+
/**
554+
* @see _:shareWith
555+
*/
556+
#[ORM\Column(type: 'simple_array', nullable: true)]
557+
#[Assert\Unique]
558+
private ?array $shareWith = [];
559+
PHP
560+
, $source);
561+
562+
$this->assertStringContainsString(<<<'PHP'
563+
public function setShareWith(?array $shareWith): void
564+
{
565+
$this->shareWith = $shareWith;
566+
}
567+
568+
public function getShareWith(): ?array
569+
{
570+
return $this->shareWith;
571+
}
572+
PHP
573+
, $source);
574+
$this->assertStringNotContainsString('function addShareWith', $source);
575+
$this->assertStringNotContainsString('function removeShareWith', $source);
576+
}
577+
541578
public function testGenerationWithoutConfigFileQuestion(): void
542579
{
543580
// No config file is given.

tests/TypesGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testGenerate(): void
105105
$article = file_get_contents("$this->outputDir/App/Entity/Article.php");
106106
$this->assertStringContainsString('abstract class Article extends CreativeWork', $article);
107107
$this->assertStringContainsString('private ?string $articleBody = null;', $article);
108-
$this->assertStringContainsString('private array $articleSection = [];', $article);
108+
$this->assertStringContainsString('private ?array $articleSection = [];', $article);
109109
$this->assertStringContainsString('public function setArticleBody(?string $articleBody): void', $article);
110110
$this->assertStringContainsString('public function getArticleBody(): ?string', $article);
111111
$this->assertStringContainsString('public function addArticleSection(string $articleSection): void', $article);

tests/config/simple-array.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
useSimpleArraySetter: true
2+
types:
3+
Project:
4+
properties:
5+
name:
6+
range: "https://schema.org/Text"
7+
shareWith:
8+
range: "https://schema.org/email"
9+
cardinality: "(0..*)"
10+
attributes:
11+
Assert\Unique: ~
12+
ORM\Column: { type: "simple_array" }

0 commit comments

Comments
 (0)