Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/cloc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: cloc
on:
pull_request:
jobs:
cloc:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
path: pr
- name: Checkout base code
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base
- name: Count Lines Of Code
id: loc
run: |
curl -OL https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "::set-output name=diff::$OUTPUT"

- name: Comment Code Lines
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: LOC
message: |
### Lines Of Code

${{ steps.loc.outputs.diff }}
37 changes: 37 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: lint
on:
push:
branches:
- master
- main
pull_request:
jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ 'ubuntu-latest' ]
php-versions: [ '7.4' ]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache vendor
uses: actions/cache@v2
with:
path: |
vendor
key: vendor-${{ hashFiles('composer.lock') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: post_max_size=256M, max_execution_time=180
tools: composer

- name: Populate vendor
run: '[ -e vendor ] || composer install'

- name: Lint
run: make lint
38 changes: 38 additions & 0 deletions .github/workflows/test-unit-cov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: test-unit-cov
on:
push:
branches:
- master
- main
pull_request:
jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ 'ubuntu-latest' ]
php-versions: [ '7.4' ]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache vendor
uses: actions/cache@v2
with:
path: |
vendor
key: vendor-${{ hashFiles('composer.lock') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: post_max_size=256M, max_execution_time=180
coverage: xdebug
tools: composer

- name: Populate vendor
run: '[ -e vendor ] || composer install'

- name: Run Tests With Coverage
run: make test-coverage && bash <(curl -s https://codecov.io/bash)
37 changes: 37 additions & 0 deletions .github/workflows/test-unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: test-unit
on:
push:
branches:
- master
- main
pull_request:
jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ 'ubuntu-latest' ]
php-versions: [ '5.6', '7.0', '7.1', '7.2', '7.3', '8.0' ]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache vendor
uses: actions/cache@v2
with:
path: |
vendor
key: vendor-${{ hashFiles('composer.lock') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
ini-values: post_max_size=256M, max_execution_time=180
tools: composer

- name: Populate vendor
run: '[ -e vendor ] || composer install'

- name: Run Tests
run: make test
37 changes: 0 additions & 37 deletions .gitlab-ci.yml

This file was deleted.

32 changes: 0 additions & 32 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.34] - 2021-07-22

### Fixes
- Handling of conflicting constant names for `enum` in generated classes.

## [0.2.33] - 2021-05-27

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

[0.2.34]: https://github.com/swaggest/php-code-builder/compare/v0.2.33...v0.2.34
[0.2.33]: https://github.com/swaggest/php-code-builder/compare/v0.2.32...v0.2.33
[0.2.32]: https://github.com/swaggest/php-code-builder/compare/v0.2.31...v0.2.32
[0.2.31]: https://github.com/swaggest/php-code-builder/compare/v0.2.30...v0.2.31
Expand Down
13 changes: 7 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion src/JsonSchema/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,18 @@ private function processEnum()
}
$value = var_export($enumItem, true);
if ($this->saveEnumConstInClass !== null && is_scalar($enumItem) && !is_bool($enumItem)) {
$this->saveEnumConstInClass->addConstant(new PhpConstant($name, $enumItem));
$checkName = $name;
$i = 1;
do {
try {
$this->saveEnumConstInClass->addConstant(new PhpConstant($checkName, $enumItem));
$name = $checkName;
break;
} catch (\Swaggest\PhpCodeBuilder\Exception $exception) {
$i++;
$checkName = $name . $i;
}
} while(true);
$this->result->addSnippet(
" self::$name,\n"
);
Expand Down
5 changes: 3 additions & 2 deletions src/PhpClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public function addConstant(PhpConstant $constant)
{
if (array_key_exists($constant->getName(), $this->constants)) {
if ($this->constants[$constant->getName()]->getValue() !== $constant->getValue()) {
throw new Exception('Duplicate const with different value');
throw new Exception('Duplicate const "' . $constant->getName() . '" with different value, ' .
$constant->getValue() . ' !== ' . $this->constants[$constant->getName()]->getValue());
}
} else {
$this->constants[$constant->getName()] = $constant;
Expand All @@ -137,8 +138,8 @@ public function addConstant(PhpConstant $constant)
* Adds a new trait to the list of traits
*
* @param PhpTrait $trait
* @throws Exception if a trait already exists with same name
* @return self
* @throws Exception if a trait already exists with same name
*/
public function addTrait(PhpTrait $trait)
{
Expand Down
62 changes: 62 additions & 0 deletions tests/src/PHPUnit/JsonSchema/AdvancedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,68 @@

class AdvancedTest extends \PHPUnit_Framework_TestCase
{
public function testEnumConst()
{
$schemaData = json_decode(<<<'JSON'
{
"properties": {
"foo": {"enum": ["a","b","B"]},
"bar": {"multipleOf": 3,"const":"A"}
}
}
JSON
);

$schema = Schema::import($schemaData);
$builder = new PhpBuilder();
$builder->makeEnumConstants = true;
$class = $builder->getClass($schema, 'Root');

$result = '';
foreach ($builder->getGeneratedClasses() as $class) {
$result .= $class->class . "\n\n";
}

$expected = <<<'JSON'
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
{
const A = 'a';

const B = 'b';

const B2 = 'B';

/** @var mixed */
public $foo;

/** @var mixed */
public $bar;

/**
* @param Swaggest\JsonSchema\Constraint\Properties|static $properties
* @param Swaggest\JsonSchema\Schema $ownerSchema
*/
public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $ownerSchema)
{
$properties->foo = new Swaggest\JsonSchema\Schema();
$properties->foo->enum = array(
self::A,
self::B,
self::B2,
);
$properties->bar = new Swaggest\JsonSchema\Schema();
$properties->bar->multipleOf = 3;
$properties->bar->const = "A";
}
}


JSON;
;

$this->assertSame($expected, $result);
}

public function testOneOf()
{
$schemaData = json_decode(<<<'JSON'
Expand Down