Skip to content

Commit

Permalink
added support for readonly classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2022
1 parent 7d90395 commit 5a40432
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 3 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nette/php-generator",
"description": "🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.1 features.",
"description": "🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.2 features.",
"keywords": ["nette", "php", "code", "scaffolding"],
"homepage": "https://nette.org",
"license": ["BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],
Expand All @@ -20,7 +20,7 @@
},
"require-dev": {
"nette/tester": "^2.4",
"nikic/php-parser": "^4.13",
"nikic/php-parser": "^4.14",
"tracy/tracy": "^2.8",
"phpstan/phpstan": "^1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function __construct(
}
```

Readonly properties introduced by PHP 8.1 can be marked via `setReadOnly()`.
Readonly properties and classes can be marked via `setReadOnly()`.

------

Expand Down
14 changes: 14 additions & 0 deletions src/PhpGenerator/ClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class ClassType extends ClassLike
private bool $final = false;
private bool $abstract = false;
private ?string $extends = null;
private bool $readOnly = false;

/** @var string[] */
private array $implements = [];
Expand Down Expand Up @@ -172,6 +173,19 @@ public function isAbstract(): bool
}


public function setReadOnly(bool $state = true): static
{
$this->readOnly = $state;
return $this;
}


public function isReadOnly(): bool
{
return $this->readOnly;
}


public function setExtends(?string $name): static
{
if ($name) {
Expand Down
1 change: 1 addition & 0 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ private function addClassToFile(PhpFile $phpFile, Node\Stmt\Class_ $node): Class

$class->setFinal($node->isFinal());
$class->setAbstract($node->isAbstract());
$class->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
$this->addCommentAndAttributes($class, $node);
return $class;
}
Expand Down
1 change: 1 addition & 0 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function fromClassReflection(
$class = new ClassType($from->getShortName(), new PhpNamespace($from->getNamespaceName()));
$class->setFinal($from->isFinal() && $class->isClass());
$class->setAbstract($from->isAbstract() && $class->isClass());
$class->setReadOnly(PHP_VERSION_ID >= 80200 && $from->isReadOnly());
}

$ifaces = $from->getInterfaceNames();
Expand Down
1 change: 1 addition & 0 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public function printClass(
if ($class instanceof ClassType) {
$line[] = $class->isAbstract() ? 'abstract' : null;
$line[] = $class->isFinal() ? 'final' : null;
$line[] = $class->isReadOnly() ? 'readonly' : null;
}

$line[] = match (true) {
Expand Down
17 changes: 17 additions & 0 deletions tests/PhpGenerator/ClassType.from.82.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @phpVersion 8.2
*/

declare(strict_types=1);

use Nette\PhpGenerator\ClassType;


require __DIR__ . '/../bootstrap.php';
require __DIR__ . '/fixtures/classes.82.php';

$res[] = ClassType::from(new Abc\Class13);

sameFile(__DIR__ . '/expected/ClassType.from.82.expect', implode("\n", $res));
3 changes: 3 additions & 0 deletions tests/PhpGenerator/Extractor.extractAll.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ sameFile(__DIR__ . '/expected/Extractor.classes.expect', (string) $file);
$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.81.php')))->extractAll();
sameFile(__DIR__ . '/expected/Extractor.classes.81.expect', (string) $file);

$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.82.php')))->extractAll();
sameFile(__DIR__ . '/expected/Extractor.classes.82.expect', (string) $file);

$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/enum.php')))->extractAll();
sameFile(__DIR__ . '/expected/Extractor.enum.expect', (string) $file);

Expand Down
3 changes: 3 additions & 0 deletions tests/PhpGenerator/expected/ClassType.from.82.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
readonly class Class13
{
}
9 changes: 9 additions & 0 deletions tests/PhpGenerator/expected/Extractor.classes.82.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Abc;

readonly class Class13
{
}
9 changes: 9 additions & 0 deletions tests/PhpGenerator/fixtures/classes.82.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Abc;

readonly class Class13
{
}

0 comments on commit 5a40432

Please sign in to comment.