Skip to content

Commit

Permalink
Type: constants are PascalCase
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2022
1 parent 6436c65 commit c2732fc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ Each type or union/intersection type can be passed as a string, you can also use
```php
use Nette\PhpGenerator\Type;

$member->setType('array'); // or Type::ARRAY;
$member->setType('array'); // or Type::Array;
$member->setType('array|string'); // or Type::union('array', 'string')
$member->setType('Foo&Bar'); // or Type::intersection(Foo::class, Bar::class)
$member->setType(null); // removes type
Expand Down
51 changes: 35 additions & 16 deletions src/PhpGenerator/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@
class Type
{
public const
STRING = 'string',
INT = 'int',
FLOAT = 'float',
BOOL = 'bool',
ARRAY = 'array',
OBJECT = 'object',
CALLABLE = 'callable',
ITERABLE = 'iterable',
VOID = 'void',
NEVER = 'never',
MIXED = 'mixed',
FALSE = 'false',
NULL = 'null',
SELF = 'self',
PARENT = 'parent',
STATIC = 'static';
String = 'string',
Int = 'int',
Float = 'float',
Bool = 'bool',
Array = 'array',
Object = 'object',
Callable = 'callable',
Iterable = 'iterable',
Void = 'void',
Never = 'never',
Mixed = 'mixed',
False = 'false',
Null = 'null',
Self = 'self',
Parent = 'parent',
Static = 'static';

/** @deprecated */
public const
STRING = self::String,
INT = self::Int,
FLOAT = self::Float,
BOOL = self::Bool,
ARRAY = self::Array,
OBJECT = self::Object,
CALLABLE = self::Callable,
ITERABLE = self::Iterable,
VOID = self::Void,
NEVER = self::Never,
MIXED = self::Mixed,
FALSE = self::False,
NULL = self::Null,
SELF = self::Self,
PARENT = self::Parent,
STATIC = self::Static;


public static function nullable(string $type, bool $state = true): string
Expand Down
8 changes: 4 additions & 4 deletions tests/PhpGenerator/ClassType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ $class->addProperty('order')
->setValue(new Literal('RecursiveIteratorIterator::SELF_FIRST'));

$class->addProperty('typed1')
->setType(Type::ARRAY)
->setType(Type::Array)
->setReadOnly();

$class->addProperty('typed2')
->setType(Type::ARRAY)
->setType(Type::Array)
->setNullable()
->setInitialized();

$class->addProperty('typed3')
->setType(Type::ARRAY)
->setType(Type::Array)
->setValue(null);

$p = $class->addProperty('sections', ['first' => true])
Expand Down Expand Up @@ -128,7 +128,7 @@ $method->addParameter('item');

$method->addParameter('res', null)
->setReference(true)
->setType(Type::union(Type::ARRAY, 'null'));
->setType(Type::union(Type::Array, 'null'));

$method->addParameter('bar', null)
->setType('stdClass|string')
Expand Down
4 changes: 2 additions & 2 deletions tests/PhpGenerator/Method.scalarParameters.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Assert::same('float', $method->getParameters()['d']->getType());

$method = (new Method('create'))
->setBody('return null;');
$method->addParameter('a')->setType(Type::STRING);
$method->addParameter('b')->setType(Type::BOOL);
$method->addParameter('a')->setType(Type::String);
$method->addParameter('b')->setType(Type::Bool);

same(
'function create(string $a, bool $b)
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/Type.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


Assert::same('A|string', Type::union(A::class, Type::STRING));
Assert::same('A|string', Type::union(A::class, Type::String));

Assert::same('?A', Type::nullable(A::class));
Assert::same('?A', Type::nullable(A::class, true));
Expand Down

0 comments on commit c2732fc

Please sign in to comment.