Skip to content

Commit e172e5e

Browse files
authored
Fix exception on duplicate constant name in enum (#42)
1 parent 7f924e4 commit e172e5e

File tree

11 files changed

+237
-78
lines changed

11 files changed

+237
-78
lines changed

.github/workflows/cloc.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: cloc
2+
on:
3+
pull_request:
4+
jobs:
5+
cloc:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Checkout code
9+
uses: actions/checkout@v2
10+
with:
11+
path: pr
12+
- name: Checkout base code
13+
uses: actions/checkout@v2
14+
with:
15+
ref: ${{ github.event.pull_request.base.sha }}
16+
path: base
17+
- name: Count Lines Of Code
18+
id: loc
19+
run: |
20+
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
21+
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
22+
OUTPUT="${OUTPUT//'%'/'%25'}"
23+
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
24+
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
25+
echo "::set-output name=diff::$OUTPUT"
26+
27+
- name: Comment Code Lines
28+
uses: marocchino/sticky-pull-request-comment@v2
29+
with:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
header: LOC
32+
message: |
33+
### Lines Of Code
34+
35+
${{ steps.loc.outputs.diff }}

.github/workflows/lint.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
jobs:
9+
run:
10+
runs-on: ${{ matrix.operating-system }}
11+
strategy:
12+
matrix:
13+
operating-system: [ 'ubuntu-latest' ]
14+
php-versions: [ '7.4' ]
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Cache vendor
20+
uses: actions/cache@v2
21+
with:
22+
path: |
23+
vendor
24+
key: vendor-${{ hashFiles('composer.lock') }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-versions }}
30+
ini-values: post_max_size=256M, max_execution_time=180
31+
tools: composer
32+
33+
- name: Populate vendor
34+
run: '[ -e vendor ] || composer install'
35+
36+
- name: Lint
37+
run: make lint

.github/workflows/test-unit-cov.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: test-unit-cov
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
jobs:
9+
run:
10+
runs-on: ${{ matrix.operating-system }}
11+
strategy:
12+
matrix:
13+
operating-system: [ 'ubuntu-latest' ]
14+
php-versions: [ '7.4' ]
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Cache vendor
20+
uses: actions/cache@v2
21+
with:
22+
path: |
23+
vendor
24+
key: vendor-${{ hashFiles('composer.lock') }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-versions }}
30+
ini-values: post_max_size=256M, max_execution_time=180
31+
coverage: xdebug
32+
tools: composer
33+
34+
- name: Populate vendor
35+
run: '[ -e vendor ] || composer install'
36+
37+
- name: Run Tests With Coverage
38+
run: make test-coverage && bash <(curl -s https://codecov.io/bash)

.github/workflows/test-unit.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: test-unit
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
pull_request:
8+
jobs:
9+
run:
10+
runs-on: ${{ matrix.operating-system }}
11+
strategy:
12+
matrix:
13+
operating-system: [ 'ubuntu-latest' ]
14+
php-versions: [ '5.6', '7.0', '7.1', '7.2', '7.3', '8.0' ]
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Cache vendor
20+
uses: actions/cache@v2
21+
with:
22+
path: |
23+
vendor
24+
key: vendor-${{ hashFiles('composer.lock') }}
25+
26+
- name: Setup PHP
27+
uses: shivammathur/setup-php@v2
28+
with:
29+
php-version: ${{ matrix.php-versions }}
30+
ini-values: post_max_size=256M, max_execution_time=180
31+
tools: composer
32+
33+
- name: Populate vendor
34+
run: '[ -e vendor ] || composer install'
35+
36+
- name: Run Tests
37+
run: make test

.gitlab-ci.yml

-37
This file was deleted.

.travis.yml

-32
This file was deleted.

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.34] - 2021-07-22
8+
9+
### Fixes
10+
- Handling of conflicting constant names for `enum` in generated classes.
11+
712
## [0.2.33] - 2021-05-27
813

914
### Fixes
@@ -93,6 +98,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9398
### Fixed
9499
- Description trimming bug.
95100

101+
[0.2.34]: https://github.com/swaggest/php-code-builder/compare/v0.2.33...v0.2.34
96102
[0.2.33]: https://github.com/swaggest/php-code-builder/compare/v0.2.32...v0.2.33
97103
[0.2.32]: https://github.com/swaggest/php-code-builder/compare/v0.2.31...v0.2.32
98104
[0.2.31]: https://github.com/swaggest/php-code-builder/compare/v0.2.30...v0.2.31

composer.lock

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JsonSchema/SchemaBuilder.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,18 @@ private function processEnum()
285285
}
286286
$value = var_export($enumItem, true);
287287
if ($this->saveEnumConstInClass !== null && is_scalar($enumItem) && !is_bool($enumItem)) {
288-
$this->saveEnumConstInClass->addConstant(new PhpConstant($name, $enumItem));
288+
$checkName = $name;
289+
$i = 1;
290+
do {
291+
try {
292+
$this->saveEnumConstInClass->addConstant(new PhpConstant($checkName, $enumItem));
293+
$name = $checkName;
294+
break;
295+
} catch (\Swaggest\PhpCodeBuilder\Exception $exception) {
296+
$i++;
297+
$checkName = $name . $i;
298+
}
299+
} while(true);
289300
$this->result->addSnippet(
290301
" self::$name,\n"
291302
);

src/PhpClass.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ public function addConstant(PhpConstant $constant)
125125
{
126126
if (array_key_exists($constant->getName(), $this->constants)) {
127127
if ($this->constants[$constant->getName()]->getValue() !== $constant->getValue()) {
128-
throw new Exception('Duplicate const with different value');
128+
throw new Exception('Duplicate const "' . $constant->getName() . '" with different value, ' .
129+
$constant->getValue() . ' !== ' . $this->constants[$constant->getName()]->getValue());
129130
}
130131
} else {
131132
$this->constants[$constant->getName()] = $constant;
@@ -137,8 +138,8 @@ public function addConstant(PhpConstant $constant)
137138
* Adds a new trait to the list of traits
138139
*
139140
* @param PhpTrait $trait
140-
* @throws Exception if a trait already exists with same name
141141
* @return self
142+
* @throws Exception if a trait already exists with same name
142143
*/
143144
public function addTrait(PhpTrait $trait)
144145
{

tests/src/PHPUnit/JsonSchema/AdvancedTest.php

+62
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,68 @@
88

99
class AdvancedTest extends \PHPUnit_Framework_TestCase
1010
{
11+
public function testEnumConst()
12+
{
13+
$schemaData = json_decode(<<<'JSON'
14+
{
15+
"properties": {
16+
"foo": {"enum": ["a","b","B"]},
17+
"bar": {"multipleOf": 3,"const":"A"}
18+
}
19+
}
20+
JSON
21+
);
22+
23+
$schema = Schema::import($schemaData);
24+
$builder = new PhpBuilder();
25+
$builder->makeEnumConstants = true;
26+
$class = $builder->getClass($schema, 'Root');
27+
28+
$result = '';
29+
foreach ($builder->getGeneratedClasses() as $class) {
30+
$result .= $class->class . "\n\n";
31+
}
32+
33+
$expected = <<<'JSON'
34+
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
35+
{
36+
const A = 'a';
37+
38+
const B = 'b';
39+
40+
const B2 = 'B';
41+
42+
/** @var mixed */
43+
public $foo;
44+
45+
/** @var mixed */
46+
public $bar;
47+
48+
/**
49+
* @param Swaggest\JsonSchema\Constraint\Properties|static $properties
50+
* @param Swaggest\JsonSchema\Schema $ownerSchema
51+
*/
52+
public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $ownerSchema)
53+
{
54+
$properties->foo = new Swaggest\JsonSchema\Schema();
55+
$properties->foo->enum = array(
56+
self::A,
57+
self::B,
58+
self::B2,
59+
);
60+
$properties->bar = new Swaggest\JsonSchema\Schema();
61+
$properties->bar->multipleOf = 3;
62+
$properties->bar->const = "A";
63+
}
64+
}
65+
66+
67+
JSON;
68+
;
69+
70+
$this->assertSame($expected, $result);
71+
}
72+
1173
public function testOneOf()
1274
{
1375
$schemaData = json_decode(<<<'JSON'

0 commit comments

Comments
 (0)