Skip to content

Commit aa5f276

Browse files
authored
Merge pull request #15 from swaggest/export-cycles
Export cycles
2 parents 4864707 + 63ee924 commit aa5f276

20 files changed

+263
-35
lines changed

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ php:
88
- 5.6
99
- 5.5
1010

11-
dist: trusty
1211
sudo: false
12+
dist: trusty
1313

1414
## Cache composer bits
1515
cache:
@@ -19,16 +19,15 @@ cache:
1919
before_script:
2020
- composer install --dev --no-interaction --prefer-dist
2121
- if ! [[ $(phpenv version-name) =~ 7.3 ]] ; then phpenv config-rm xdebug.ini || true ; fi
22-
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then 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; fi
2322

2423
matrix:
2524
allow_failures:
2625
- php: nightly
2726
fast_finish: true
2827

2928
script:
30-
- php -derror_reporting="E_ALL & ~E_DEPRECATED" ./vendor/bin/phpunit -v --configuration phpunit.xml --coverage-text --coverage-clover=coverage.xml
31-
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then php $HOME/.cache/composer/phpstan-0.11.8.phar analyze -l 7 -c phpstan.neon ./src; fi
29+
- if [[ $(phpenv version-name) =~ 7.3 ]] ; then make test-coverage; else make test; fi
30+
- if [[ $(phpenv version-name) =~ 7.2 ]] ; then make lint; fi
3231

3332
after_script:
3433
- if [[ $(phpenv version-name) =~ 7.3 ]] ; then bash <(curl -s https://codecov.io/bash); fi

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
PHPSTAN_VERSION ?= 0.11.15
2+
13
deps:
24
@git submodule init && git submodule update
35

46
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+
@test -f ${HOME}/.cache/composer/phpstan-${PHPSTAN_VERSION}.phar || (mkdir -p ${HOME}/.cache/composer/ && wget https://github.com/phpstan/phpstan/releases/download/${PHPSTAN_VERSION}/phpstan.phar -O ${HOME}/.cache/composer/phpstan-${PHPSTAN_VERSION}.phar)
8+
@php $$HOME/.cache/composer/phpstan-${PHPSTAN_VERSION}.phar analyze -l 7 -c phpstan.neon ./src
79

810
docker-lint:
911
@docker run -v $$PWD:/app --rm phpstan/phpstan analyze -l 7 -c phpstan.neon ./src
@@ -12,7 +14,7 @@ test:
1214
@php -derror_reporting="E_ALL & ~E_DEPRECATED" vendor/bin/phpunit
1315

1416
test-coverage:
15-
@php -derror_reporting="E_ALL & ~E_DEPRECATED" -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-text
17+
@php -derror_reporting="E_ALL & ~E_DEPRECATED" -dzend_extension=xdebug.so vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
1618

1719
gen:
1820
@php ./tools/generate_swagger_structures.php

composer.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JsonSchema/SchemaExporterInterface.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use Swaggest\CodeBuilder\PlaceholderString;
66
use Swaggest\JsonSchema\Schema;
77
use Swaggest\JsonSchema\SchemaExporter;
8-
use Swaggest\JsonSchema\Wrapper;
98
use Swaggest\PhpCodeBuilder\PhpClass;
9+
use Swaggest\PhpCodeBuilder\PhpClassProperty;
1010
use Swaggest\PhpCodeBuilder\PhpCode;
11+
use Swaggest\PhpCodeBuilder\PhpFlags;
1112
use Swaggest\PhpCodeBuilder\PhpFunction;
1213
use Swaggest\PhpCodeBuilder\PhpInterface;
1314
use Swaggest\PhpCodeBuilder\Types\TypeOf;
@@ -43,10 +44,28 @@ public function process(PhpClass $class, $path, $schema)
4344
}
4445

4546
if (count($propertiesFound) > 5) {
47+
$prop = new PhpClassProperty(
48+
'schemaStorage',
49+
PhpClass::byFQN(\SplObjectStorage::class),
50+
PhpFlags::VIS_PRIVATE
51+
);
52+
$prop->setIsStatic(true);
53+
$prop->setDescription('Schema storage keeps exported schemas to avoid infinite cycle recursions.');
54+
$class->addProperty($prop);
55+
4656
$func = new PhpFunction('exportSchema');
4757
$func->setResult(PhpClass::byFQN(Schema::class));
4858
$body = (new PhpCode())->addSnippet(new PlaceholderString(<<<PHP
49-
\$schema = new :schema();
59+
if (null === self::\$schemaStorage) {
60+
self::\$schemaStorage = new SplObjectStorage();
61+
}
62+
63+
if (self::\$schemaStorage->contains(\$this)) {
64+
return self::\$schemaStorage->offsetGet(\$this);
65+
} else {
66+
\$schema = new :schema();
67+
self::\$schemaStorage->attach(\$this, \$schema);
68+
}
5069
5170
PHP
5271
, [':schema' => new TypeOf(PhpClass::byFQN(Schema::class))]

tests/src/Tmp/Swagger/DefinitionsSchema.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Swagger;
88

9+
use SplObjectStorage;
910
use Swaggest\JsonSchema\Constraint\Properties;
1011
use Swaggest\JsonSchema\Exception\StringException;
1112
use Swaggest\JsonSchema\Helper;
@@ -113,6 +114,9 @@ class DefinitionsSchema extends ClassStructure implements SchemaExporter
113114
/** @var mixed */
114115
public $example;
115116

117+
/** @var SplObjectStorage Schema storage keeps exported schemas to avoid infinite cycle recursions. */
118+
private static $schemaStorage;
119+
116120
/**
117121
* @param Properties|static $properties
118122
* @param Schema $ownerSchema
@@ -571,7 +575,16 @@ public function setXValue($name, $value)
571575
*/
572576
function exportSchema()
573577
{
574-
$schema = new Schema();
578+
if (null === self::$schemaStorage) {
579+
self::$schemaStorage = new SplObjectStorage();
580+
}
581+
582+
if (self::$schemaStorage->contains($this)) {
583+
return self::$schemaStorage->offsetGet($this);
584+
} else {
585+
$schema = new Schema();
586+
self::$schemaStorage->attach($this, $schema);
587+
}
575588
$schema->ref = $this->ref;
576589
$schema->format = $this->format;
577590
$schema->title = $this->title;

tests/src/Tmp/Swagger/FileSchema.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Swagger;
88

9+
use SplObjectStorage;
910
use Swaggest\JsonSchema\Constraint\Properties;
1011
use Swaggest\JsonSchema\Exception\StringException;
1112
use Swaggest\JsonSchema\Helper;
@@ -52,6 +53,9 @@ class FileSchema extends ClassStructure implements SchemaExporter
5253
/** @var mixed */
5354
public $example;
5455

56+
/** @var SplObjectStorage Schema storage keeps exported schemas to avoid infinite cycle recursions. */
57+
private static $schemaStorage;
58+
5559
/**
5660
* @param Properties|static $properties
5761
* @param Schema $ownerSchema
@@ -229,7 +233,16 @@ public function setXValue($name, $value)
229233
*/
230234
function exportSchema()
231235
{
232-
$schema = new Schema();
236+
if (null === self::$schemaStorage) {
237+
self::$schemaStorage = new SplObjectStorage();
238+
}
239+
240+
if (self::$schemaStorage->contains($this)) {
241+
return self::$schemaStorage->offsetGet($this);
242+
} else {
243+
$schema = new Schema();
244+
self::$schemaStorage->attach($this, $schema);
245+
}
233246
$schema->format = $this->format;
234247
$schema->title = $this->title;
235248
$schema->description = $this->description;

tests/src/Tmp/Swagger/FormDataParameterSubSchema.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Swagger;
88

9+
use SplObjectStorage;
910
use Swaggest\JsonSchema\Constraint\Properties;
1011
use Swaggest\JsonSchema\Exception\StringException;
1112
use Swaggest\JsonSchema\Helper;
@@ -102,6 +103,9 @@ class FormDataParameterSubSchema extends ClassStructure implements SchemaExporte
102103
/** @var float */
103104
public $multipleOf;
104105

106+
/** @var SplObjectStorage Schema storage keeps exported schemas to avoid infinite cycle recursions. */
107+
private static $schemaStorage;
108+
105109
/**
106110
* @param Properties|static $properties
107111
* @param Schema $ownerSchema
@@ -457,7 +461,16 @@ public function setXValue($name, $value)
457461
*/
458462
function exportSchema()
459463
{
460-
$schema = new Schema();
464+
if (null === self::$schemaStorage) {
465+
self::$schemaStorage = new SplObjectStorage();
466+
}
467+
468+
if (self::$schemaStorage->contains($this)) {
469+
return self::$schemaStorage->offsetGet($this);
470+
} else {
471+
$schema = new Schema();
472+
self::$schemaStorage->attach($this, $schema);
473+
}
461474
$schema->description = $this->description;
462475
$schema->type = $this->type;
463476
$schema->format = $this->format;

tests/src/Tmp/Swagger/Header.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Swagger;
88

9+
use SplObjectStorage;
910
use Swaggest\JsonSchema\Constraint\Properties;
1011
use Swaggest\JsonSchema\Exception\StringException;
1112
use Swaggest\JsonSchema\Helper;
@@ -86,6 +87,9 @@ class Header extends ClassStructure implements SchemaExporter
8687
/** @var string */
8788
public $description;
8889

90+
/** @var SplObjectStorage Schema storage keeps exported schemas to avoid infinite cycle recursions. */
91+
private static $schemaStorage;
92+
8993
/**
9094
* @param Properties|static $properties
9195
* @param Schema $ownerSchema
@@ -382,7 +386,16 @@ public function setXValue($name, $value)
382386
*/
383387
function exportSchema()
384388
{
385-
$schema = new Schema();
389+
if (null === self::$schemaStorage) {
390+
self::$schemaStorage = new SplObjectStorage();
391+
}
392+
393+
if (self::$schemaStorage->contains($this)) {
394+
return self::$schemaStorage->offsetGet($this);
395+
} else {
396+
$schema = new Schema();
397+
self::$schemaStorage->attach($this, $schema);
398+
}
386399
$schema->type = $this->type;
387400
$schema->format = $this->format;
388401
if ($this->items !== null && $this->items instanceof SchemaExporter) {

tests/src/Tmp/Swagger/HeaderParameterSubSchema.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Swagger;
88

9+
use SplObjectStorage;
910
use Swaggest\JsonSchema\Constraint\Properties;
1011
use Swaggest\JsonSchema\Exception\StringException;
1112
use Swaggest\JsonSchema\Helper;
@@ -97,6 +98,9 @@ class HeaderParameterSubSchema extends ClassStructure implements SchemaExporter
9798
/** @var float */
9899
public $multipleOf;
99100

101+
/** @var SplObjectStorage Schema storage keeps exported schemas to avoid infinite cycle recursions. */
102+
private static $schemaStorage;
103+
100104
/**
101105
* @param Properties|static $properties
102106
* @param Schema $ownerSchema
@@ -436,7 +440,16 @@ public function setXValue($name, $value)
436440
*/
437441
function exportSchema()
438442
{
439-
$schema = new Schema();
443+
if (null === self::$schemaStorage) {
444+
self::$schemaStorage = new SplObjectStorage();
445+
}
446+
447+
if (self::$schemaStorage->contains($this)) {
448+
return self::$schemaStorage->offsetGet($this);
449+
} else {
450+
$schema = new Schema();
451+
self::$schemaStorage->attach($this, $schema);
452+
}
440453
$schema->description = $this->description;
441454
$schema->type = $this->type;
442455
$schema->format = $this->format;

tests/src/Tmp/Swagger/PathParameterSubSchema.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Swagger;
88

9+
use SplObjectStorage;
910
use Swaggest\JsonSchema\Constraint\Properties;
1011
use Swaggest\JsonSchema\Exception\StringException;
1112
use Swaggest\JsonSchema\Helper;
@@ -97,6 +98,9 @@ class PathParameterSubSchema extends ClassStructure implements SchemaExporter
9798
/** @var float */
9899
public $multipleOf;
99100

101+
/** @var SplObjectStorage Schema storage keeps exported schemas to avoid infinite cycle recursions. */
102+
private static $schemaStorage;
103+
100104
/**
101105
* @param Properties|static $properties
102106
* @param Schema $ownerSchema
@@ -441,7 +445,16 @@ public function setXValue($name, $value)
441445
*/
442446
function exportSchema()
443447
{
444-
$schema = new Schema();
448+
if (null === self::$schemaStorage) {
449+
self::$schemaStorage = new SplObjectStorage();
450+
}
451+
452+
if (self::$schemaStorage->contains($this)) {
453+
return self::$schemaStorage->offsetGet($this);
454+
} else {
455+
$schema = new Schema();
456+
self::$schemaStorage->attach($this, $schema);
457+
}
445458
$schema->description = $this->description;
446459
$schema->type = $this->type;
447460
$schema->format = $this->format;

0 commit comments

Comments
 (0)