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

Lines changed: 35 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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
Lines changed: 38 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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

Lines changed: 0 additions & 37 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
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

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

src/JsonSchema/SchemaBuilder.php

Lines changed: 12 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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
{

0 commit comments

Comments
 (0)