Skip to content
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

Fix namespace sorting of capitalcase #120

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
8 changes: 4 additions & 4 deletions .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
name: Nette Code Checker
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v1
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
coverage: none
Expand All @@ -21,8 +21,8 @@ jobs:
name: Nette Coding Standard
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v1
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
coverage: none
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
name: PHPStan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

name: PHP ${{ matrix.php }} tests
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
Expand All @@ -22,7 +22,7 @@ jobs:
- run: composer install --no-progress --prefer-dist
- run: vendor/bin/tester tests -s -C
- if: failure()
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: output
path: tests/**/output
Expand All @@ -32,7 +32,7 @@ jobs:
name: Lowest Dependencies
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
Expand All @@ -46,7 +46,7 @@ jobs:
name: Code Coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
Expand Down
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.15",
"tracy/tracy": "^2.8",
"phpstan/phpstan": "^1.0"
},
Expand Down
37 changes: 16 additions & 21 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: '#^Match expression does not handle remaining value\: true$#'
count: 1
path: src/PhpGenerator/ClassLike.php

-
message: '#^Method Nette\\PhpGenerator\\ClassType\:\:addTrait\(\) has parameter \$deprecatedParam with no value type specified in iterable type array\.$#'
count: 1
Expand All @@ -15,16 +10,6 @@ parameters:
count: 1
path: src/PhpGenerator/EnumType.php

-
message: '#^Access to an undefined property PhpParser\\Node\:\:\$attrGroups\.$#'
count: 1
path: src/PhpGenerator/Extractor.php

-
message: '#^Access to an undefined property PhpParser\\Node\\Expr\:\:\$value\.$#'
count: 1
path: src/PhpGenerator/Extractor.php

-
message: '#^Call to an undefined method Nette\\PhpGenerator\\ClassLike\:\:addConstant\(\)\.$#'
count: 1
Expand All @@ -45,13 +30,18 @@ parameters:
count: 1
path: src/PhpGenerator/Extractor.php

-
message: '#^Call to an undefined method Nette\\PhpGenerator\\FunctionLike\:\:addPromotedParameter\(\)\.$#'
count: 1
path: src/PhpGenerator/Extractor.php

-
message: '#^Method Nette\\PhpGenerator\\Extractor\:\:addCommentAndAttributes\(\) has parameter \$element with no type specified\.$#'
count: 1
path: src/PhpGenerator/Extractor.php

-
message: '#^Property class@anonymous/PhpGenerator/Extractor\.php\:176\:\:\$callback has no type specified\.$#'
message: '#^Property class@anonymous/PhpGenerator/Extractor\.php\:175\:\:\$callback has no type specified\.$#'
count: 1
path: src/PhpGenerator/Extractor.php

Expand All @@ -65,6 +55,11 @@ parameters:
count: 1
path: src/PhpGenerator/Factory.php

-
message: '#^Call to an undefined method ReflectionClass\<object\>\:\:isReadOnly\(\)\.$#'
count: 1
path: src/PhpGenerator/Factory.php

-
message: '#^Elseif branch is unreachable because previous condition is always true\.$#'
count: 1
Expand All @@ -90,11 +85,6 @@ parameters:
count: 1
path: src/PhpGenerator/Factory.php

-
message: '#^Parameter \#1 \$name of method Nette\\PhpGenerator\\ClassType\:\:setExtends\(\) expects string\|null, array\<int, string\> given\.$#'
count: 1
path: src/PhpGenerator/Factory.php

-
message: '#^Unreachable statement \- code above always terminates\.$#'
count: 1
Expand All @@ -115,6 +105,11 @@ parameters:
count: 1
path: src/PhpGenerator/Method.php

-
message: '#^Method Nette\\PhpGenerator\\Printer\:\:printDocComment\(\) has parameter \$commentable with no type specified\.$#'
count: 1
path: src/PhpGenerator/Printer.php

-
message: '#^Method Nette\\PhpGenerator\\TraitType\:\:addTrait\(\) has parameter \$deprecatedParam with no value type specified in iterable type array\.$#'
count: 1
Expand Down
4 changes: 2 additions & 2 deletions 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 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
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
5 changes: 1 addition & 4 deletions src/PhpGenerator/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@

/**
* Closure.
*
* @property-deprecated string $body
*/
final class Closure
final class Closure extends FunctionLike
{
use Nette\SmartObject;
use Traits\FunctionLike;
use Traits\AttributeAware;

/** @var Parameter[] */
Expand Down
15 changes: 12 additions & 3 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function getReformattedContents(array $nodes, int $level): string
*/
private function prepareReplacements(array $nodes): array
{
$start = $nodes[0]->getStartFilePos();
$start = $this->getNodeStartPos($nodes[0]);
$replacements = [];
(new NodeFinder)->find($nodes, function (Node $node) use (&$replacements, $start) {
if ($node instanceof Node\Name\FullyQualified) {
Expand Down 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 Expand Up @@ -384,7 +385,7 @@ private function addCommentAndAttributes($element, Node $node): void
}


private function setupFunction(GlobalFunction|Method $function, Node\FunctionLike $node): void
private function setupFunction(FunctionLike $function, Node\FunctionLike $node): void
{
$function->setReturnReference($node->returnsByRef());
$function->setReturnType($node->getReturnType() ? $this->toPhp($node->getReturnType()) : null);
Expand Down Expand Up @@ -430,7 +431,15 @@ private function toPhp(mixed $value): string

private function getNodeContents(Node ...$nodes): string
{
$start = $nodes[0]->getStartFilePos();
$start = $this->getNodeStartPos($nodes[0]);
return substr($this->code, $start, end($nodes)->getEndFilePos() - $start + 1);
}


private function getNodeStartPos(Node $node): int
{
return ($comments = $node->getComments())
? $comments[0]->getStartFilePos()
: $node->getStartFilePos();
}
}
24 changes: 13 additions & 11 deletions src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function fromClassReflection(
\ReflectionClass $from,
bool $withBodies = false,
?bool $materializeTraits = null,
): ClassLike {
): ClassLike
{
if ($materializeTraits !== null) {
trigger_error(__METHOD__ . '() parameter $materializeTraits has been removed (is always false).', E_USER_DEPRECATED);
}
Expand All @@ -55,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 All @@ -70,7 +72,7 @@ public function fromClassReflection(
}

$class->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$class->setAttributes(self::getAttributes($from));
$class->setAttributes($this->getAttributes($from));
if ($from->getParentClass()) {
$class->setExtends($from->getParentClass()->name);
$class->setImplements(array_diff($class->getImplements(), $from->getParentClass()->getInterfaceNames()));
Expand Down Expand Up @@ -160,7 +162,7 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
$method->setReturnReference($from->returnsReference());
$method->setVariadic($from->isVariadic());
$method->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$method->setAttributes(self::getAttributes($from));
$method->setAttributes($this->getAttributes($from));
$method->setReturnType((string) $from->getReturnType());

return $method;
Expand All @@ -177,7 +179,7 @@ public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody
$function->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
}

$function->setAttributes(self::getAttributes($from));
$function->setAttributes($this->getAttributes($from));
$function->setReturnType((string) $from->getReturnType());

if ($withBody) {
Expand All @@ -192,12 +194,12 @@ public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody
}


public function fromCallable(callable $from): Method|GlobalFunction|Closure
public function fromCallable(callable $from): FunctionLike
{
$ref = Nette\Utils\Callback::toReflection($from);
return $ref instanceof \ReflectionMethod
? self::fromMethodReflection($ref)
: self::fromFunctionReflection($ref);
? $this->fromMethodReflection($ref)
: $this->fromFunctionReflection($ref);
}


Expand Down Expand Up @@ -226,7 +228,7 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
$param->setNullable($param->isNullable() && $param->getDefaultValue() !== null);
}

$param->setAttributes(self::getAttributes($from));
$param->setAttributes($this->getAttributes($from));
return $param;
}

Expand All @@ -238,7 +240,7 @@ public function fromConstantReflection(\ReflectionClassConstant $from): Constant
$const->setVisibility($this->getVisibility($from));
$const->setFinal(PHP_VERSION_ID >= 80100 ? $from->isFinal() : false);
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$const->setAttributes(self::getAttributes($from));
$const->setAttributes($this->getAttributes($from));
return $const;
}

Expand All @@ -248,7 +250,7 @@ public function fromCaseReflection(\ReflectionClassConstant $from): EnumCase
$const = new EnumCase($from->name);
$const->setValue($from->getValue()->value ?? null);
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$const->setAttributes(self::getAttributes($from));
$const->setAttributes($this->getAttributes($from));
return $const;
}

Expand All @@ -265,7 +267,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
$prop->setReadOnly(PHP_VERSION_ID >= 80100 ? $from->isReadOnly() : false);
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
$prop->setAttributes(self::getAttributes($from));
$prop->setAttributes($this->getAttributes($from));
return $prop;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

declare(strict_types=1);

namespace Nette\PhpGenerator\Traits;
namespace Nette\PhpGenerator;

use Nette;
use Nette\PhpGenerator\Dumper;
use Nette\PhpGenerator\Parameter;
use Nette\Utils\Type;


/**
* @internal
* GlobalFunction/Closure/Method description.
*
* @property-deprecated string $body
*/
trait FunctionLike
abstract class FunctionLike
{
private string $body = '';

Expand Down