Skip to content

Commit 51b71e8

Browse files
authored
Merge pull request #10 from swaggest/lint-issues
Make generated code more phpstan-friendly
2 parents 9856eef + 286da31 commit 51b71e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+502
-268
lines changed

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
deps:
2+
@git submodule init && git submodule update
3+
4+
lint:
5+
@test -f $$HOME/.cache/composer/phpstan-0.11.8.phar || wget https://github.com/phpstan/phpstan/releases/download/0.11.8/phpstan.phar -O $$HOME/.cache/composer/phpstan-0.11.8.phar
6+
@php $$HOME/.cache/composer/phpstan-0.11.8.phar analyze -l 7 -c phpstan.neon ./src
7+
8+
docker-lint:
9+
@docker run -v $$PWD:/app --rm phpstan/phpstan analyze -l 7 -c phpstan.neon ./src
10+
11+
test:
12+
@php -derror_reporting="E_ALL & ~E_DEPRECATED" vendor/bin/phpunit
13+
14+
test-coverage:
15+
@php -derror_reporting="E_ALL & ~E_DEPRECATED" -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-text
16+
17+
gen:
18+
@php ./tools/generate_swagger_structures.php
19+
@php ./tools/generate_status_codes.php

src/JsonSchema/SchemaBuilder.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,29 @@ private function processLogic()
383383
foreach (array($names->anyOf, $names->oneOf, $names->allOf) as $keyword) {
384384
if ($this->schema->$keyword !== null) {
385385
foreach ($this->schema->$keyword as $index => $schema) {
386-
$this->result->addSnippet(
387-
$this->copyTo(new SchemaBuilder(
386+
$varName = '$' . PhpCode::makePhpName("{$this->varName}->{$keyword}[{$index}]");
387+
$schemaInit = $this->copyTo(new SchemaBuilder(
388+
$schema,
389+
$varName,
390+
$this->path . "->{$keyword}[{$index}]",
391+
$this->phpBuilder
392+
))->build();
393+
394+
if (count($schemaInit->snippets) === 1) { // Init in single statement can be just assigned.
395+
$this->result->addSnippet($this->copyTo(new SchemaBuilder(
388396
$schema,
389397
"{$this->varName}->{$keyword}[{$index}]",
390398
$this->path . "->{$keyword}[{$index}]",
391399
$this->phpBuilder
392-
))->build()
393-
);
400+
))->build());
401+
} else {
402+
$this->result->addSnippet($schemaInit);
403+
$this->result->addSnippet(<<<PHP
404+
{$this->varName}->{$keyword}[{$index}] = {$varName};
405+
406+
PHP
407+
);
408+
}
394409
}
395410
}
396411
}

src/JsonSchema/SchemaExporterInterface.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,57 @@ public function process(PhpClass $class, $path, $schema)
5252
, [':schema' => new TypeOf(PhpClass::byFQN(Schema::class))]
5353
));
5454

55+
$names = Schema::names();
5556
foreach ($propertiesFound as $name) {
57+
if ($name === $names->items
58+
|| $name === $names->additionalProperties
59+
|| $name === $names->additionalItems
60+
|| $name === $names->not
61+
|| $name === $names->if
62+
|| $name === $names->then
63+
|| $name === $names->else) {
64+
$body->addSnippet(<<<PHP
65+
if (\$this->$name !== null && \$this->$name instanceof SchemaExporter) {
66+
\$schema->$name = \$this->{$name}->exportSchema();
67+
}
68+
69+
PHP
70+
);
71+
continue;
72+
}
73+
74+
if ($name === $names->allOf || $name === $names->oneOf || $name === $names->anyOf) {
75+
$body->addSnippet(<<<PHP
76+
if (!empty(\$this->$name)) {
77+
foreach (\$this->$name as \$i => \$item) {
78+
if (\$item instanceof SchemaExporter) {
79+
\$schema->{$name}[\$i] = \$item->exportSchema();
80+
}
81+
}
82+
}
83+
84+
PHP
85+
);
86+
continue;
87+
}
88+
89+
90+
if ($name === $names->properties) {
91+
$body->addSnippet(<<<PHP
92+
if (!empty(\$this->$name)) {
93+
foreach (\$this->$name as \$propertyName => \$propertySchema) {
94+
if (is_string(\$propertyName) && \$propertySchema instanceof SchemaExporter) {
95+
\$schema->setProperty(\$propertyName, \$propertySchema->exportSchema());
96+
}
97+
}
98+
}
99+
100+
PHP
101+
);
102+
continue;
103+
}
104+
105+
56106
$body->addSnippet(<<<PHP
57107
\$schema->$name = \$this->$name;
58108

src/Types/OrType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Swaggest\PhpCodeBuilder\Types;
44

55
use Swaggest\PhpCodeBuilder\PhpAnyType;
6+
use Swaggest\PhpCodeBuilder\PhpStdType;
67

78
class OrType implements PhpAnyType
89
{
@@ -26,10 +27,19 @@ public function renderPhpDocType()
2627
foreach ($this->types as $type) {
2728
$phpDocType = $type->renderPhpDocType();
2829
if ($phpDocType) {
29-
$index[$phpDocType] = 1;
30+
$phpDocParts = explode('|', $phpDocType);
31+
foreach ($phpDocParts as $k => $part) {
32+
if ($part !== PhpStdType::TYPE_MIXED) {
33+
$index[$phpDocType] = 1;
34+
}
35+
}
3036
}
3137
}
32-
return implode('|', array_keys($index));
38+
$phpDocType = implode('|', array_keys($index));
39+
if ($phpDocType === '') {
40+
$phpDocType = PhpStdType::TYPE_MIXED;
41+
}
42+
return $phpDocType;
3343
}
3444

3545
/**

tests/src/PHPUnit/JsonSchema/AdvancedTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
5252
5353
class RootOneOf0 extends Swaggest\JsonSchema\Structure\ClassStructure
5454
{
55+
/** @var mixed */
5556
public $foo;
5657
58+
/** @var mixed */
5759
public $bar;
5860
5961
/**
@@ -73,8 +75,10 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
7375
7476
class RootOneOf1 extends Swaggest\JsonSchema\Structure\ClassStructure
7577
{
78+
/** @var mixed */
7679
public $foo;
7780
81+
/** @var mixed */
7882
public $bar;
7983
8084
/**
@@ -144,8 +148,10 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
144148
145149
class RootAnyOf0 extends Swaggest\JsonSchema\Structure\ClassStructure
146150
{
151+
/** @var mixed */
147152
public $foo;
148153
154+
/** @var mixed */
149155
public $bar;
150156
151157
/**
@@ -165,8 +171,10 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
165171
166172
class RootAnyOf1 extends Swaggest\JsonSchema\Structure\ClassStructure
167173
{
174+
/** @var mixed */
168175
public $foo;
169176
177+
/** @var mixed */
170178
public $bar;
171179
172180
/**
@@ -237,8 +245,10 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
237245
238246
class RootAllOf0 extends Swaggest\JsonSchema\Structure\ClassStructure
239247
{
248+
/** @var mixed */
240249
public $foo;
241250
251+
/** @var mixed */
242252
public $bar;
243253
244254
/**
@@ -258,8 +268,10 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
258268
259269
class RootAllOf1 extends Swaggest\JsonSchema\Structure\ClassStructure
260270
{
271+
/** @var mixed */
261272
public $foo;
262273
274+
/** @var mixed */
263275
public $bar;
264276
265277
/**
@@ -308,7 +320,7 @@ public function testNot()
308320

309321
$expected = <<<'PHP'
310322
/**
311-
* @method static import($data, Swaggest\JsonSchema\Context $options = null)
323+
* @method static mixed import($data, Swaggest\JsonSchema\Context $options = null)
312324
*/
313325
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
314326
{
@@ -324,8 +336,10 @@ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $
324336
325337
class RootNot extends Swaggest\JsonSchema\Structure\ClassStructure
326338
{
339+
/** @var mixed */
327340
public $foo;
328341
342+
/** @var mixed */
329343
public $bar;
330344
331345
/**
@@ -407,7 +421,7 @@ public function testPatternProperties()
407421

408422
$expected = <<<'PHP'
409423
/**
410-
* @method static import($data, Swaggest\JsonSchema\Context $options = null)
424+
* @method static mixed import($data, Swaggest\JsonSchema\Context $options = null)
411425
*/
412426
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
413427
{

tests/src/Tmp/Issue5/IfClass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class IfClass extends ClassStructure
1515
{
1616
const BAR = 'bar';
1717

18+
/** @var mixed */
1819
public $foo;
1920

2021
/**

tests/src/Tmp/Swagger/ApiKeySecurity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function getXValues()
134134

135135
/**
136136
* @param string $name
137-
* @param $value
137+
* @param mixed $value
138138
* @return self
139139
* @throws InvalidValue
140140
* @codeCoverageIgnoreStart

tests/src/Tmp/Swagger/BasicAuthenticationSecurity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getXValues()
9292

9393
/**
9494
* @param string $name
95-
* @param $value
95+
* @param mixed $value
9696
* @return self
9797
* @throws InvalidValue
9898
* @codeCoverageIgnoreStart

tests/src/Tmp/Swagger/BodyParameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function getXValues()
147147

148148
/**
149149
* @param string $name
150-
* @param $value
150+
* @param mixed $value
151151
* @return self
152152
* @throws InvalidValue
153153
* @codeCoverageIgnoreStart

tests/src/Tmp/Swagger/Contact.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function getXValues()
107107

108108
/**
109109
* @param string $name
110-
* @param $value
110+
* @param mixed $value
111111
* @return self
112112
* @throws InvalidValue
113113
* @codeCoverageIgnoreStart

0 commit comments

Comments
 (0)