Skip to content

Commit

Permalink
Rename to scalarAndReturnTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Nov 20, 2020
1 parent 3ad7ba9 commit 432e8eb
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion config/app.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'CakeDto' => [
'format' => 'xml', // or your custom yml, neon, ...
'strictTypes' => false, // This can create casting requirement
'scalarTypeHints' => true,
'scalarAndReturnTypes' => true,
'immutable' => false, // This can have a negative performance impact
'defaultCollectionType' => null, // Defaults to \ArrayObject
'debug' => false, // Add all meta data into DTOs for debugging
Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ You can set some defaults via `app.php` and global Configure settings:
return [
'Dto' => [
'strictTypes' => false, // This can require additional casting
'scalarTypeHints' => true,
'scalarAndReturnTypes' => true,
'immutable' => false, // This can have a negative performance impact
'defaultCollectionType' => null, // Defaults to \ArrayObject
],
Expand Down Expand Up @@ -593,7 +593,7 @@ See the examples for details.


## Scalar Param and Return Types
Types for scalars are added by default. Use Configure and `'CakeDto.scalarTypeHints'` set to `false` to disable this.
Types for scalars are added by default. Use Configure and `'CakeDto.scalarAndReturnTypes'` set to `false` to disable this.


## Strict Types
Expand Down
10 changes: 5 additions & 5 deletions src/Generator/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct(EngineInterface $engine) {

$this->simpleTypeWhitelist = $this->simpleTypeWhitelist($this->simpleTypeWhitelist);
$config = [
'scalarTypeHints' => Configure::read('CakeDto.scalarTypeHints', true),
'scalarAndReturnTypes' => Configure::read('CakeDto.scalarAndReturnTypes', true),
'defaultCollectionType' => Configure::read('CakeDto.defaultCollectionType', '\ArrayObject'),
'debug' => (bool)Configure::read('CakeDto.debug'),
'immutable' => (bool)Configure::read('CakeDto.immutable'),
Expand Down Expand Up @@ -446,11 +446,11 @@ protected function _completeMeta(array $dto, string $namespace): array {
}
}

if ($fields[$key]['typeHint'] && $this->_config['scalarTypeHints']) {
if ($fields[$key]['typeHint'] && $this->_config['scalarAndReturnTypes']) {
$fields[$key]['returnTypeHint'] = $fields[$key]['typeHint'];
}

if ($fields[$key]['typeHint'] && $this->_config['scalarTypeHints'] && $fields[$key]['nullable']) {
if ($fields[$key]['typeHint'] && $this->_config['scalarAndReturnTypes'] && $fields[$key]['nullable']) {
$fields[$key]['typeHint'] = '?' . $fields[$key]['typeHint'];
}

Expand All @@ -464,7 +464,7 @@ protected function _completeMeta(array $dto, string $namespace): array {
$fields[$key]['singularTypeHint'] = $this->typehint($fields[$key]['singularType']);
}

if ($fields[$key]['singularTypeHint'] && $this->_config['scalarTypeHints']) {
if ($fields[$key]['singularTypeHint'] && $this->_config['scalarAndReturnTypes']) {
$fields[$key]['singularReturnTypeHint'] = $fields[$key]['singularTypeHint'];
}
}
Expand Down Expand Up @@ -695,7 +695,7 @@ protected function typehint(string $type): ?string {
if (in_array($type, $this->simpleTypeAdditionsForDocBlock, true)) {
return null;
}
if (!$this->_config['scalarTypeHints'] && in_array($type, $this->simpleTypeWhitelist, true)) {
if (!$this->_config['scalarAndReturnTypes'] && in_array($type, $this->simpleTypeWhitelist, true)) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/View/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function getView(): View {
*/
protected function setGlobalConfiguration(): void {
$strictTypes = (bool)Configure::read('CakeDto.strictTypes', false);
$scalarTypeHints = (bool)Configure::read('CakeDto.scalarTypeHints', true);
$scalarAndReturnTypes = (bool)Configure::read('CakeDto.scalarAndReturnTypes', true);

$this->set(compact('strictTypes', 'scalarTypeHints'));
$this->set(compact('strictTypes', 'scalarAndReturnTypes'));
}

}
2 changes: 1 addition & 1 deletion templates/Dto/element/constants.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* @deprecated {{ field.deprecated }}
*/
{% endif %}
{% if scalarTypeHints %}public {% endif %}const FIELD_{{ field.name | underscore | upper }} = '{{ field.name }}';
{% if scalarAndReturnTypes %}public {% endif %}const FIELD_{{ field.name | underscore | upper }} = '{{ field.name }}';
{% endfor -%}
4 changes: 2 additions & 2 deletions templates/Dto/element/method_has.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% endif %}
* @return bool
*/
public function has{{ name[:1]|upper ~ name[1:] }}(){% if scalarTypeHints %}: bool{% endif %} {
public function has{{ name[:1]|upper ~ name[1:] }}(){% if scalarAndReturnTypes %}: bool{% endif %} {
{% if collectionType %}
if ($this->{{ name }} === null) {
return false;
Expand All @@ -31,7 +31,7 @@
* @param string|int $key
* @return bool
*/
public function has{{ singular[:1]|upper ~ singular[1:] }}($key){% if scalarTypeHints %}: bool{% endif %} {
public function has{{ singular[:1]|upper ~ singular[1:] }}($key){% if scalarAndReturnTypes %}: bool{% endif %} {
return isset($this->{{ name }}[$key]);
}
{% endif -%}
2 changes: 1 addition & 1 deletion templates/Dto/element/method_set.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @return $this
*/
public function set{{ name[:1]|upper ~ name[1:] }}({% if typeHint %}{{ typeHint }} {% endif %}${{ name }}{% if typeHint and nullable and not scalarTypeHints %} = null{% endif %}) {
public function set{{ name[:1]|upper ~ name[1:] }}({% if typeHint %}{{ typeHint }} {% endif %}${{ name }}{% if typeHint and nullable and not scalarAndReturnTypes %} = null{% endif %}) {
$this->{{ name }} = ${{ name }};
$this->_touchedFields[self::FIELD_{{ name | underscore | upper }}] = true;

Expand Down
28 changes: 14 additions & 14 deletions tests/TestCase/Generator/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testBasic() {
$exampleXml = ROOT . DS . 'docs/examples/basic.dto.xml';
copy($exampleXml, $this->configPath . 'dto.xml');

Configure::write('CakeDto.scalarTypeHints', false);
Configure::write('CakeDto.scalarAndReturnTypes', false);

$options = [
'confirm' => true,
Expand All @@ -95,7 +95,7 @@ public function testBasic() {
$this->assertTemplateContains('CarDto.has', $file);
$this->assertTemplateContains('CarDto.get_or_fail', $file);

Configure::delete('CakeDto.scalarTypeHints');
Configure::delete('CakeDto.scalarAndReturnTypes');
}

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ public function testDeprecated() {
$xml = ROOT . DS . 'tests/files/xml/deprecated.xml';
copy($xml, $this->configPath . 'dto.xml');

Configure::write('CakeDto.scalarTypeHints', false);
Configure::write('CakeDto.scalarAndReturnTypes', false);

$options = [
'confirm' => true,
Expand All @@ -145,7 +145,7 @@ public function testDeprecated() {
$this->assertTemplateContains('AnimalDto.dto', $file);
$this->assertTemplateContainsCount('@deprecated', 1, $file);

Configure::delete('CakeDto.scalarTypeHints');
Configure::delete('CakeDto.scalarAndReturnTypes');
}

/**
Expand Down Expand Up @@ -186,7 +186,7 @@ class MySpecialTreeDto extends \App\Dto\MyForest\MyTreeDto {
/**
* @return void
*/
public function testScalarTypeHints() {
public function testscalarAndReturnTypes() {
$xml = ROOT . DS . 'docs/examples/basic.dto.xml';
copy($xml, $this->configPath . 'dto.xml');

Expand All @@ -201,13 +201,13 @@ public function testScalarTypeHints() {
$this->assertSame(2, $result);

$file = $this->srcPath . 'Dto' . DS . 'CarDto.php';
$this->assertTemplateContains('ScalarTypeHints/CarDto.setDistanceTravelled', $file);
$this->assertTemplateContains('ScalarAndReturnTypes/CarDto.setDistanceTravelled', $file);

$file = $this->srcPath . 'Dto' . DS . 'FlyingCarDto.php';
$this->assertTemplateContains('ScalarTypeHints/FlyingCarDto.constant', $file);
$this->assertTemplateContains('ScalarTypeHints/FlyingCarDto.setMaxAltitude', $file);
$this->assertTemplateContains('ScalarTypeHints/FlyingCarDto.getMaxSpeed', $file);
$this->assertTemplateContains('ScalarTypeHints/FlyingCarDto.hasComplexAttributes', $file);
$this->assertTemplateContains('ScalarAndReturnTypes/FlyingCarDto.constant', $file);
$this->assertTemplateContains('ScalarAndReturnTypes/FlyingCarDto.setMaxAltitude', $file);
$this->assertTemplateContains('ScalarAndReturnTypes/FlyingCarDto.getMaxSpeed', $file);
$this->assertTemplateContains('ScalarAndReturnTypes/FlyingCarDto.hasComplexAttributes', $file);
$this->assertStringNotContainsString(' getMaxAltitudeOrFail(', file_get_contents($file));
$this->assertStringNotContainsString(' getMaxSpeedOrFail(', file_get_contents($file));
$this->assertStringContainsString(' getComplexAttributesOrFail(', file_get_contents($file));
Expand All @@ -218,17 +218,17 @@ public function testScalarTypeHints() {

// Assoc
$file = $this->srcPath . 'Dto' . DS . 'CarsDto.php';
$this->assertTemplateContains('ScalarTypeHints/CarsDto.addCar', $file);
$this->assertTemplateContains('ScalarAndReturnTypes/CarsDto.addCar', $file);
}

/**
* @return void
*/
public function testScalarTypeHintsDefaultValueRequiredFalse() {
public function testscalarAndReturnTypesDefaultValueRequiredFalse() {
$xml = ROOT . DS . 'tests/files/xml/scalar_default_required_false.xml';
copy($xml, $this->configPath . 'dto.xml');

Configure::write('CakeDto.scalarTypeHints', true);
Configure::write('CakeDto.scalarAndReturnTypes', true);
$this->generator = $this->createGenerator();

$options = [
Expand All @@ -240,7 +240,7 @@ public function testScalarTypeHintsDefaultValueRequiredFalse() {
$this->assertSame(2, $result);

$file = $this->srcPath . 'Dto' . DS . 'DefaultValueDto.php';
$this->assertTemplateContains('ScalarTypeHints/DefaultValueDto.setDefaultedOptionalField', $file);
$this->assertTemplateContains('ScalarAndReturnTypes/DefaultValueDto.setDefaultedOptionalField', $file);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use CakeDto\Generator\Generator;
use CakeDto\View\Renderer;

Configure::write('CakeDto.scalarTypeHints', true);
Configure::write('CakeDto.scalarAndReturnTypes', true);
Configure::write('CakeDto.strictTypes', false);

$engine = new XmlEngine();
Expand Down

0 comments on commit 432e8eb

Please sign in to comment.