Skip to content

Commit acd5e23

Browse files
author
roadiz-ci
committed
chore: bumped
1 parent 83ebc2b commit acd5e23

11 files changed

Lines changed: 104 additions & 57 deletions

.github/workflows/run-test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
runs-on: ubuntu-latest
2121
strategy:
2222
matrix:
23-
php-version: ['8.2', '8.3']
23+
php-version: ['8.1', '8.2', '8.3']
2424
steps:
2525
- uses: shivammathur/setup-php@v2
2626
with:
@@ -36,5 +36,7 @@ jobs:
3636
${{ runner.os }}-php-${{ matrix.php-version }}-
3737
- name: Install Dependencies
3838
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
39+
- name: Run PHP Code Sniffer
40+
run: vendor/bin/phpcs -p ./src
3941
- name: Run PHPStan
4042
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test:
2+
vendor/bin/phpcbf --report=full --report-file=./report.txt -p ./
3+
vendor/bin/phpstan analyse -c phpstan.neon
4+

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"description": "Roadiz sub-package which generates Typescript interfaces skeleton based on your schema",
44
"type": "library",
55
"require": {
6-
"roadiz/nodetype-contracts": "^2.0.0",
6+
"roadiz/nodetype-contracts": "~1.1.2",
77
"symfony/http-foundation": "6.4.*"
88
},
99
"require-dev": {
1010
"phpstan/phpstan": "^1.5.3",
11-
"phpstan/phpdoc-parser": "<2"
11+
"squizlabs/php_codesniffer": "^3.5"
1212
},
1313
"license": "MIT",
1414
"authors": [
@@ -26,8 +26,8 @@
2626
},
2727
"extra": {
2828
"branch-alias": {
29-
"dev-master": "2.5.x-dev",
30-
"dev-develop": "2.6.x-dev"
29+
"dev-master": "2.3.x-dev",
30+
"dev-develop": "2.4.x-dev"
3131
}
3232
}
3333
}

phpcs.xml.dist

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
4+
5+
<arg name="basepath" value="."/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors"/>
8+
<arg name="extensions" value="php"/>
9+
10+
<rule ref="PSR12">
11+
<exclude name="Generic.Files.LineLength"/>
12+
</rule>
13+
<file>./src</file>
14+
<exclude-pattern>*/node_modules</exclude-pattern>
15+
<exclude-pattern>*/.AppleDouble</exclude-pattern>
16+
<exclude-pattern>*/vendor</exclude-pattern>
17+
</ruleset>

src/DeclarationGeneratorFactory.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
use RZ\Roadiz\Typescript\Declaration\Generators\ScalarFieldGenerator;
1616
use Symfony\Component\HttpFoundation\ParameterBag;
1717

18-
final readonly class DeclarationGeneratorFactory
18+
final class DeclarationGeneratorFactory
1919
{
20-
public function __construct(private ParameterBag $nodeTypesBag)
20+
public function __construct(private readonly ParameterBag $nodeTypesBag)
2121
{
2222
}
2323

24+
/**
25+
* @return ParameterBag
26+
*/
2427
public function getNodeTypesBag(): ParameterBag
2528
{
2629
return $this->nodeTypesBag;
@@ -31,6 +34,11 @@ public function getHumanBool(bool $bool): string
3134
return $bool ? 'true' : 'false';
3235
}
3336

37+
/**
38+
* @param NodeTypeInterface $nodeType
39+
*
40+
* @return NodeTypeGenerator
41+
*/
3442
public function createForNodeType(NodeTypeInterface $nodeType): NodeTypeGenerator
3543
{
3644
return new NodeTypeGenerator(
@@ -39,6 +47,11 @@ public function createForNodeType(NodeTypeInterface $nodeType): NodeTypeGenerato
3947
);
4048
}
4149

50+
/**
51+
* @param NodeTypeFieldInterface $field
52+
*
53+
* @return AbstractFieldGenerator
54+
*/
4255
public function createForNodeTypeField(NodeTypeFieldInterface $field): AbstractFieldGenerator
4356
{
4457
return match (true) {

src/Generators/AbstractFieldGenerator.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@ abstract class AbstractFieldGenerator
1212
// Four spaces
1313
public const INDENTATION_MARK = ' ';
1414

15+
protected NodeTypeFieldInterface $field;
16+
protected ParameterBag $nodeTypesBag;
17+
18+
/**
19+
* @param NodeTypeFieldInterface $field
20+
* @param ParameterBag $nodeTypesBag
21+
*/
1522
public function __construct(
16-
protected readonly NodeTypeFieldInterface $field,
17-
protected readonly ParameterBag $nodeTypesBag,
23+
NodeTypeFieldInterface $field,
24+
ParameterBag $nodeTypesBag
1825
) {
26+
$this->field = $field;
27+
$this->nodeTypesBag = $nodeTypesBag;
1928
}
2029

2130
protected function getNullableAssertion(): string
@@ -27,18 +36,18 @@ abstract protected function getType(): string;
2736

2837
private function getDeclaration(): string
2938
{
30-
return static::INDENTATION_MARK.
31-
$this->field->getVarName().
32-
$this->getNullableAssertion().': '.
39+
return static::INDENTATION_MARK .
40+
$this->field->getVarName() .
41+
$this->getNullableAssertion() . ': ' .
3342
$this->getType();
3443
}
3544

3645
public function getContents(): string
3746
{
3847
return implode(PHP_EOL, [
39-
$this->getIntroduction(),
40-
$this->getDeclaration(),
41-
]);
48+
$this->getIntroduction(),
49+
$this->getDeclaration()
50+
]);
4251
}
4352

4453
protected function getIntroductionLines(): array
@@ -51,16 +60,19 @@ protected function getIntroductionLines(): array
5160
}
5261

5362
if (!empty($this->field->getGroupName())) {
54-
$lines[] = 'Group: '.$this->field->getGroupName();
63+
$lines[] = 'Group: ' . $this->field->getGroupName();
5564
}
5665

5766
return $lines;
5867
}
5968

69+
/**
70+
* @return string
71+
*/
6072
public function getIntroduction(): string
6173
{
6274
return implode(PHP_EOL, array_map(function (string $line) {
63-
return static::INDENTATION_MARK.'// '.$line;
75+
return static::INDENTATION_MARK . '// ' . $line;
6476
}, $this->getIntroductionLines()));
6577
}
6678
}

src/Generators/ChildrenNodeFieldGenerator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ public function getContents(): string
1212
$this->getIntroduction(),
1313
]);
1414
}
15-
1615
protected function getIntroductionLines(): array
1716
{
1817
$lines = [
19-
'This node-type uses "blocks" which are available through parent RoadizNodesSources.blocks',
18+
'This node-type uses "blocks" which are available through parent RoadizNodesSources.blocks'
2019
];
21-
if (!empty($this->field->getDefaultValuesAsArray())) {
22-
$lines[] = 'Possible block node-types: '.json_encode($this->field->getDefaultValuesAsArray());
20+
if (!empty($this->field->getDefaultValues())) {
21+
$lines[] = 'Possible block node-types: ' . $this->field->getDefaultValues();
2322
}
24-
2523
return $lines;
2624
}
2725

src/Generators/DeclarationGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ final class DeclarationGenerator
1515
private array $nodeTypes;
1616

1717
/**
18+
* @param DeclarationGeneratorFactory $generatorFactory
1819
* @param NodeTypeInterface[] $nodeTypes
1920
*/
2021
public function __construct(
2122
private readonly DeclarationGeneratorFactory $generatorFactory,
22-
array $nodeTypes = [],
23+
array $nodeTypes = []
2324
) {
2425
if (empty($nodeTypes)) {
2526
$this->nodeTypes = array_unique($this->generatorFactory->getNodeTypesBag()->all());
@@ -40,7 +41,7 @@ public function getContents(): string
4041

4142
$blocks[] = $this->getAllTypesInterface();
4243

43-
return implode(PHP_EOL.PHP_EOL, $blocks);
44+
return implode(PHP_EOL . PHP_EOL, $blocks);
4445
}
4546

4647
private function getAllTypesInterface(): string

src/Generators/EnumFieldGenerator.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ protected function getType(): string
1010
{
1111
switch (true) {
1212
case $this->field->isEnum():
13-
$defaultValues = $this->field->getDefaultValuesAsArray();
14-
if (!empty($defaultValues) && count($defaultValues) > 0) {
15-
$defaultValues = array_map(function (string $value) {
16-
return '\''.$value.'\'';
17-
}, $defaultValues);
18-
19-
return implode(' | ', $defaultValues).' | null';
13+
if (!empty($this->field->getDefaultValues())) {
14+
$defaultValues = array_filter(
15+
array_map(
16+
'trim',
17+
explode(',', $this->field->getDefaultValues())
18+
)
19+
);
20+
if (count($defaultValues) > 0) {
21+
$defaultValues = array_map(function (string $value) {
22+
return '\'' . $value . '\'';
23+
}, $defaultValues);
24+
return implode(' | ', $defaultValues) . ' | null';
25+
}
2026
}
21-
2227
return 'string';
2328
case $this->field->isMultiple():
2429
return 'Array<string>';
@@ -30,10 +35,9 @@ protected function getType(): string
3035
protected function getIntroductionLines(): array
3136
{
3237
$lines = parent::getIntroductionLines();
33-
if (!empty($this->field->getDefaultValuesAsArray())) {
34-
$lines[] = 'Possible values: '.json_encode($this->field->getDefaultValuesAsArray());
38+
if (!empty($this->field->getDefaultValues())) {
39+
$lines[] = 'Possible values: ' . $this->field->getDefaultValues();
3540
}
36-
3741
return $lines;
3842
}
3943
}

src/Generators/NodeReferencesFieldGenerator.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@ protected function getNullableAssertion(): string
1515

1616
protected function getType(): string
1717
{
18-
return 'Array<'.$this->getUnionType().'>';
18+
return 'Array<' . $this->getUnionType() . '>';
1919
}
2020

2121
/**
2222
* @return array<NodeTypeInterface>
2323
*/
2424
private function getLinkedNodeTypes(): array
2525
{
26-
$nodeTypeNames = $this->field->getDefaultValuesAsArray();
27-
28-
if (0 === count($nodeTypeNames)) {
29-
return $nodeTypeNames;
26+
if (null === $this->field->getDefaultValues()) {
27+
return [];
3028
}
31-
29+
$nodeTypeNames = explode(',', $this->field->getDefaultValues());
3230
return array_values(array_filter(array_map(function (string $name) {
3331
$nodeType = $this->nodeTypesBag->get(trim($name));
34-
3532
return $nodeType instanceof NodeTypeInterface ? $nodeType : null;
3633
}, $nodeTypeNames)));
3734
}
@@ -52,10 +49,9 @@ private function getUnionType(): string
5249
protected function getIntroductionLines(): array
5350
{
5451
$lines = parent::getIntroductionLines();
55-
if (!empty($this->field->getDefaultValuesAsArray())) {
56-
$lines[] = 'Possible values: '.json_encode($this->field->getDefaultValuesAsArray());
52+
if (!empty($this->field->getDefaultValues())) {
53+
$lines[] = 'Possible values: ' . $this->field->getDefaultValues();
5754
}
58-
5955
return $lines;
6056
}
6157
}

0 commit comments

Comments
 (0)