Skip to content

Commit bed5a1e

Browse files
committed
added Type
1 parent c537463 commit bed5a1e

File tree

7 files changed

+81
-9
lines changed

7 files changed

+81
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
install:
3838
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress
3939
script:
40-
- php temp/coding-standard/ecs check src tests --config temp/coding-standard/coding-standard-php71.yml
40+
- php temp/coding-standard/ecs check src tests --config tests/coding-standard.yml
4141

4242

4343
- stage: Static Analysis (informative)

readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,25 @@ Members can be removed using `removeProperty()`, `removeConstant()`, `removeMeth
127127
PHP Generator supports all new PHP 7.3 and 7.4 features:
128128

129129
```php
130+
use Nette\PhpGenerator\Type;
131+
130132
$class = new Nette\PhpGenerator\ClassType('Demo');
131133

132134
$class->addConstant('ID', 123)
133135
->setPrivate(); // constant visiblity
134136

135137
$class->addProperty('items')
136-
->setType('array') // typed properites
138+
->setType(Type::ARRAY) // typed properites
137139
->setNullable()
138140
->setInitialized();
139141

140142
$method = $class->addMethod('getValue')
141-
->setReturnType('int') // method return type
143+
->setReturnType(Type::INT) // method return type
142144
->setReturnNullable() // nullable return type
143145
->setBody('return count($this->items);');
144146

145147
$method->addParameter('id')
146-
->setType('int') // scalar type hint
148+
->setType(Type::ARRAY) // scalar type hint
147149
->setNullable(); // nullable type hint
148150

149151
echo $class;

src/PhpGenerator/Type.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\PhpGenerator;
11+
12+
13+
/**
14+
* PHP return, property and parameter types.
15+
*/
16+
class Type
17+
{
18+
public const
19+
STRING = 'string',
20+
INT = 'int',
21+
FLOAT = 'float',
22+
BOOL = 'bool',
23+
ARRAY = 'array',
24+
OBJECT = 'object',
25+
CALLABLE = 'callable',
26+
ITERABLE = 'iterable',
27+
VOID = 'void',
28+
NULL = 'null',
29+
SELF = 'self',
30+
PARENT = 'parent';
31+
32+
33+
public static function nullable(string $type, bool $state = true): string
34+
{
35+
return ($state ? '?' : '') . ltrim($type, '?');
36+
}
37+
38+
39+
public static function union(string ...$types): string
40+
{
41+
return implode('|', $types);
42+
}
43+
}

tests/PhpGenerator/ClassType.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare(strict_types=1);
88

99
use Nette\PhpGenerator\ClassType;
1010
use Nette\PhpGenerator\PhpLiteral;
11+
use Nette\PhpGenerator\Type;
1112
use Tester\Assert;
1213

1314

@@ -57,10 +58,10 @@ $class->addProperty('order')
5758
->setValue(new PhpLiteral('RecursiveIteratorIterator::SELF_FIRST'));
5859

5960
$class->addProperty('typed1')
60-
->setType('array');
61+
->setType(Type::ARRAY);
6162

6263
$class->addProperty('typed2')
63-
->setType('array')
64+
->setType(Type::ARRAY)
6465
->setNullable()
6566
->setInitialized();
6667

@@ -120,7 +121,7 @@ $method->addParameter('item');
120121

121122
$method->addParameter('res', null)
122123
->setReference(true)
123-
->setType('array');
124+
->setType(Type::ARRAY);
124125

125126
sameFile(__DIR__ . '/expected/ClassType.expect', (string) $class);
126127

tests/PhpGenerator/Method.scalarParameters.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare(strict_types=1);
88

99

1010
use Nette\PhpGenerator\Method;
11+
use Nette\PhpGenerator\Type;
1112
use Tester\Assert;
1213

1314

@@ -38,8 +39,8 @@ Assert::same('float', $method->getParameters()['d']->getType());
3839

3940
$method = (new Method('create'))
4041
->setBody('return null;');
41-
$method->addParameter('a')->setType('string');
42-
$method->addParameter('b')->setType('bool');
42+
$method->addParameter('a')->setType(Type::STRING);
43+
$method->addParameter('b')->setType(Type::BOOL);
4344

4445
same(
4546
'function create(string $a, bool $b)

tests/PhpGenerator/Type.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\Type;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
11+
Assert::same('A|string', Type::union(A::class, Type::STRING));
12+
13+
Assert::same('?A', Type::nullable(A::class));
14+
Assert::same('?A', Type::nullable(A::class, true));
15+
Assert::same('A', Type::nullable(A::class, false));
16+
17+
Assert::same('?A', Type::nullable('?A', true));
18+
Assert::same('A', Type::nullable('?A', false));

tests/coding-standard.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
imports:
2+
- { resource: '../temp/coding-standard/coding-standard-php71.yml' }
3+
4+
parameters:
5+
skip:
6+
PhpCsFixer\Fixer\Casing\LowercaseConstantsFixer:
7+
- src/PhpGenerator/Type.php # constant NULL

0 commit comments

Comments
 (0)