diff --git a/specs/func-declaration/global.php b/specs/func-declaration/global.php index c74e5805..b196ca87 100644 --- a/specs/func-declaration/global.php +++ b/specs/func-declaration/global.php @@ -112,14 +112,36 @@ class Y {} use ArrayIterator; function foo( - Foo $arg0, - \Foo $arg1, - Foo\Bar $arg2, - \Foo\Bar $arg3, - ArrayIterator $arg4, - \ArrayIterator $arg5, - X\Y $arg6, - \X\Y $arg7 + string $arg0, + ?string $arg1, + ?string $arg2 = null, + + Foo $arg3, + ?Foo $arg4, + Foo $arg5 = null, + + \Foo $arg6, + ?\Foo $arg7, + \Foo $arg8 = null, + + Foo\Bar $arg9, + ?Foo\Bar $arg10, + Foo\Bar $arg11 = null, + + \Foo\Bar $arg7, + ?\Foo\Bar $arg12, + \Foo\Bar $arg13 = null, + + ArrayIterator $arg14, + ?ArrayIterator $arg15, + ?ArrayIterator $arg16 = null, + + \ArrayIterator $arg17, + ?\ArrayIterator $arg18, + \ArrayIterator $arg19 = null, + + X\Y $arg20, + \X\Y $arg21 ) {} } @@ -146,7 +168,7 @@ class_alias('Humbug\\X\\Y', 'X\\Y', \false); use Humbug\Foo; use ArrayIterator; -function foo(\Humbug\Foo $arg0, \Humbug\Foo $arg1, \Humbug\Foo\Bar $arg2, \Humbug\Foo\Bar $arg3, \ArrayIterator $arg4, \ArrayIterator $arg5, \Humbug\X\Y $arg6, \Humbug\X\Y $arg7) +function foo(string $arg0, ?string $arg1, ?string $arg2 = null, \Humbug\Foo $arg3, ?\Humbug\Foo $arg4, \Humbug\Foo $arg5 = null, \Humbug\Foo $arg6, ?\Humbug\Foo $arg7, \Humbug\Foo $arg8 = null, \Humbug\Foo\Bar $arg9, ?\Humbug\Foo\Bar $arg10, \Humbug\Foo\Bar $arg11 = null, \Humbug\Foo\Bar $arg7, ?\Humbug\Foo\Bar $arg12, \Humbug\Foo\Bar $arg13 = null, \ArrayIterator $arg14, ?\ArrayIterator $arg15, ?\ArrayIterator $arg16 = null, \ArrayIterator $arg17, ?\ArrayIterator $arg18, \ArrayIterator $arg19 = null, \Humbug\X\Y $arg20, \Humbug\X\Y $arg21) { } @@ -182,7 +204,6 @@ class Y {} use ArrayIterator; function foo(): void {} - function foo(): null {} function foo(): bool {} function foo(): ?bool {} @@ -246,9 +267,6 @@ class_alias('Humbug\\X\\Y', 'X\\Y', \false); function foo() : void { } -function foo() : null -{ -} function foo() : bool { } @@ -291,40 +309,40 @@ function foo() : callable function foo() : ?callable { } -function foo() : Foo +function foo() : \Humbug\Foo { } -function foo() : \Foo +function foo() : \Humbug\Foo { } -function foo() : ?Foo +function foo() : ?\Humbug\Foo { } -function foo() : ?\Foo +function foo() : ?\Humbug\Foo { } -function foo() : ArrayIterator +function foo() : \ArrayIterator { } function foo() : \ArrayIterator { } -function foo() : ?ArrayIterator +function foo() : ?\ArrayIterator { } function foo() : ?\ArrayIterator { } -function foo() : X\Y +function foo() : \Humbug\X\Y { } -function foo() : \X\Y +function foo() : \Humbug\X\Y { } -function foo() : ?X\Y +function foo() : ?\Humbug\X\Y { } -function foo() : ?\X\Y +function foo() : ?\Humbug\X\Y { } diff --git a/specs/func-declaration/method.php b/specs/func-declaration/method.php new file mode 100644 index 00000000..91663baf --- /dev/null +++ b/specs/func-declaration/method.php @@ -0,0 +1,287 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return [ + 'meta' => [ + 'title' => 'Method declarations', + // Default values. If not specified will be the one used + 'prefix' => 'Humbug', + 'whitelist' => [], + ], + + [ + 'spec' => <<<'SPEC' +Method declarations: +- prefix the namespace statements +- prefix the appropriate classes +SPEC + , + 'whitelist' => ['X\Y'], + 'payload' => <<<'PHP' + <<<'SPEC' +Method declarations with return types: +- prefix namespace statements +- prefix the appropriate classes +- append the class_alias statement to the whitelisted class +SPEC + , + 'whitelist' => ['X\Y'], + 'payload' => <<<'PHP' +nameResolver->resolveName($name); $resolvedName = $resolvedValue->getName(); @@ -150,6 +167,10 @@ private function prefixName(Name $name): Node return $resolvedName; } + if ('self' === (string) $resolvedName && $parentNode instanceof ClassMethod) { + return $name; + } + return FullyQualified::concat( $this->prefix, $resolvedName->toString(), diff --git a/src/NodeVisitor/Resolver/FullyQualifiedNameResolver.php b/src/NodeVisitor/Resolver/FullyQualifiedNameResolver.php index a676eafd..2394895f 100644 --- a/src/NodeVisitor/Resolver/FullyQualifiedNameResolver.php +++ b/src/NodeVisitor/Resolver/FullyQualifiedNameResolver.php @@ -17,10 +17,12 @@ use Humbug\PhpScoper\NodeVisitor\AppendParentNode; use Humbug\PhpScoper\NodeVisitor\Collection\NamespaceStmtCollection; use Humbug\PhpScoper\NodeVisitor\Collection\UseStmtCollection; +use Humbug\PhpScoper\NodeVisitor\NameStmtPrefixer; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; +use function in_array; /** * Attempts to resolve the node name into a fully qualified node. Returns a valid (non fully-qualified) name node on @@ -64,6 +66,10 @@ private function resolveNodeName(Name $name, ?Name $namespace, ?Name $use): Name return new FullyQualified($name, $name->getAttributes()); } + if (in_array((string) $name, NameStmtPrefixer::PHP_FUNCTION_KEYWORDS, true)) { + return $name; + } + $parentNode = AppendParentNode::getParent($name); if (