Skip to content

Add failing test for array #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
install:
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress
script:
- php temp/coding-standard/ecs check src tests --config temp/coding-standard/coding-standard-php71.yml
- php temp/coding-standard/ecs check src tests --config tests/coding-standard.yml


- stage: Static Analysis (informative)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 7.4 features.",
"keywords": ["nette", "php", "code", "scaffolding"],
"homepage": "https://nette.org",
"license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"],
"license": ["BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],
"authors": [
{
"name": "David Grudl",
Expand Down
36 changes: 19 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ $class = new Nette\PhpGenerator\ClassType('Demo');

$class
->setFinal()
->setExtends('ParentClass')
->addImplement('Countable')
->addTrait('Nette\SmartObject')
->setExtends(ParentClass::class)
->addImplement(Countable::class)
->addTrait(Nette\SmartObject::class)
->addComment("Description of class.\nSecond line\n")
->addComment('@property-read Nette\Forms\Form $form');

Expand Down Expand Up @@ -78,7 +78,7 @@ We can add constants and properties:
$class->addConstant('ID', 123);

$class->addProperty('items', [1, 2, 3])
->setVisibility('private')
->setPrivate()
->setStatic()
->addComment('@var int[]');
```
Expand All @@ -99,7 +99,7 @@ $method = $class->addMethod('count')
->addComment('Count it.')
->addComment('@return int')
->setFinal()
->setVisibility('protected')
->setProtected()
->setBody('return count($items ?: $this->items);');

$method->addParameter('items', []) // $items = []
Expand Down Expand Up @@ -127,23 +127,25 @@ Members can be removed using `removeProperty()`, `removeConstant()`, `removeMeth
PHP Generator supports all new PHP 7.3 and 7.4 features:

```php
use Nette\PhpGenerator\Type;

$class = new Nette\PhpGenerator\ClassType('Demo');

$class->addConstant('ID', 123)
->setVisibility('private'); // constant visiblity
->setPrivate(); // constant visiblity

$class->addProperty('items')
->setType('array') // typed properites
->setType(Type::ARRAY) // typed properites
->setNullable()
->setInitialized();

$method = $class->addMethod('getValue')
->setReturnType('int') // method return type
->setReturnType(Type::INT) // method return type
->setReturnNullable() // nullable return type
->setBody('return count($this->items);');

$method->addParameter('id')
->setType('int') // scalar type hint
->setType(Type::ARRAY) // scalar type hint
->setNullable(); // nullable type hint

echo $class;
Expand Down Expand Up @@ -205,17 +207,17 @@ It can be used also for functions, closures, namespaces etc.
Literals
--------

You can pass any PHP code to property or parameter default values via `PhpLiteral`:
You can pass any PHP code to property or parameter default values via `Literal`:

```php
use Nette\PhpGenerator\PhpLiteral;
use Nette\PhpGenerator\Literal;

$class = new Nette\PhpGenerator\ClassType('Demo');

$class->addProperty('foo', new PhpLiteral('Iterator::SELF_FIRST'));
$class->addProperty('foo', new Literal('Iterator::SELF_FIRST'));

$class->addMethod('bar')
->addParameter('id', new PhpLiteral('1 + 2'));
->addParameter('id', new Literal('1 + 2'));

echo $class;
```
Expand All @@ -238,8 +240,8 @@ Interface or Trait

```php
$class = new Nette\PhpGenerator\ClassType('DemoInterface');
$class->setType('interface');
// or $class->setType('trait');
$class->setInterface();
// or $class->setTrait();
```

Trait Resolutions and Visibility
Expand Down Expand Up @@ -439,8 +441,8 @@ If the class already exists, it will be overwritten.
You can define use-statements:

```php
$namespace->addUse('Http\Request'); // use Http\Request;
$namespace->addUse('Http\Request', 'HttpReq'); // use Http\Request as HttpReq;
$namespace->addUse(Http\Request::class); // use Http\Request;
$namespace->addUse(Http\Request::class, 'HttpReq'); // use Http\Request as HttpReq;
```

**IMPORTANT NOTE:** when the class is part of the namespace, it is rendered slightly differently: all types (ie. type hints, return types, parent class name,
Expand Down
121 changes: 63 additions & 58 deletions src/PhpGenerator/ClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,14 @@ public function __toString(): string
}


/**
* Deprecated: an object can be in multiple namespaces.
* @deprecated
*/
/** @deprecated an object can be in multiple namespaces */
public function getNamespace(): ?PhpNamespace
{
return $this->namespace;
}


/**
* @return static
*/
/** @return static */
public function setName(?string $name): self
{
if ($name !== null && !Helpers::isIdentifier($name)) {
Expand All @@ -126,9 +121,49 @@ public function getName(): ?string
}


/**
* @return static
*/
/** @return static */
public function setClass(): self
{
$this->type = self::TYPE_CLASS;
return $this;
}


public function isClass(): bool
{
return $this->type === self::TYPE_CLASS;
}


/** @return static */
public function setInterface(): self
{
$this->type = self::TYPE_INTERFACE;
return $this;
}


public function isInterface(): bool
{
return $this->type === self::TYPE_INTERFACE;
}


/** @return static */
public function setTrait(): self
{
$this->type = self::TYPE_TRAIT;
return $this;
}


public function isTrait(): bool
{
return $this->type === self::TYPE_TRAIT;
}


/** @return static */
public function setType(string $type): self
{
if (!in_array($type, [self::TYPE_CLASS, self::TYPE_INTERFACE, self::TYPE_TRAIT], true)) {
Expand All @@ -145,9 +180,7 @@ public function getType(): string
}


/**
* @return static
*/
/** @return static */
public function setFinal(bool $state = true): self
{
$this->final = $state;
Expand All @@ -161,9 +194,7 @@ public function isFinal(): bool
}


/**
* @return static
*/
/** @return static */
public function setAbstract(bool $state = true): self
{
$this->abstract = $state;
Expand Down Expand Up @@ -192,18 +223,14 @@ public function setExtends($names): self
}


/**
* @return string|string[]
*/
/** @return string|string[] */
public function getExtends()
{
return $this->extends;
}


/**
* @return static
*/
/** @return static */
public function addExtend(string $name): self
{
$this->validateNames([$name]);
Expand All @@ -225,18 +252,14 @@ public function setImplements(array $names): self
}


/**
* @return string[]
*/
/** @return string[] */
public function getImplements(): array
{
return $this->implements;
}


/**
* @return static
*/
/** @return static */
public function addImplement(string $name): self
{
$this->validateNames([$name]);
Expand All @@ -257,27 +280,21 @@ public function setTraits(array $names): self
}


/**
* @return string[]
*/
/** @return string[] */
public function getTraits(): array
{
return array_keys($this->traits);
}


/**
* @internal
*/
/** @internal */
public function getTraitResolutions(): array
{
return $this->traits;
}


/**
* @return static
*/
/** @return static */
public function addTrait(string $name, array $resolutions = []): self
{
$this->validateNames([$name]);
Expand All @@ -293,7 +310,7 @@ public function addTrait(string $name, array $resolutions = []): self
public function addMember($member): self
{
if ($member instanceof Method) {
if ($this->type === self::TYPE_INTERFACE) {
if ($this->isInterface()) {
$member->setBody(null);
}
$this->methods[$member->getName()] = $member;
Expand Down Expand Up @@ -327,9 +344,7 @@ public function setConstants(array $consts): self
}


/**
* @return Constant[]
*/
/** @return Constant[] */
public function getConstants(): array
{
return $this->consts;
Expand All @@ -342,9 +357,7 @@ public function addConstant(string $name, $value): Constant
}


/**
* @return static
*/
/** @return static */
public function removeConstant(string $name): self
{
unset($this->consts[$name]);
Expand All @@ -369,9 +382,7 @@ public function setProperties(array $props): self
}


/**
* @return Property[]
*/
/** @return Property[] */
public function getProperties(): array
{
return $this->properties;
Expand Down Expand Up @@ -430,9 +441,7 @@ public function setMethods(array $methods): self
}


/**
* @return Method[]
*/
/** @return Method[] */
public function getMethods(): array
{
return $this->methods;
Expand All @@ -451,18 +460,16 @@ public function getMethod(string $name): Method
public function addMethod(string $name): Method
{
$method = new Method($name);
if ($this->type === self::TYPE_INTERFACE) {
if ($this->isInterface()) {
$method->setBody(null);
} else {
$method->setVisibility(self::VISIBILITY_PUBLIC);
$method->setPublic();
}
return $this->methods[$name] = $method;
}


/**
* @return static
*/
/** @return static */
public function removeMethod(string $name): self
{
unset($this->methods[$name]);
Expand All @@ -476,9 +483,7 @@ public function hasMethod(string $name): bool
}


/**
* @throws Nette\InvalidStateException
*/
/** @throws Nette\InvalidStateException */
public function validate(): void
{
if ($this->abstract && $this->final) {
Expand Down
4 changes: 1 addition & 3 deletions src/PhpGenerator/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ final class Constant
private $value;


/**
* @return static
*/
/** @return static */
public function setValue($val): self
{
$this->value = $val;
Expand Down
Loading