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

Add support for Attributes #738

Merged
merged 3 commits into from
Nov 9, 2022
Merged
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
80 changes: 80 additions & 0 deletions specs/misc/attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'meta' => [
'title' => 'Attributes',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',

'expose-global-constants' => false,
'expose-global-classes' => false,
'expose-global-functions' => false,
'expose-namespaces' => [],
'expose-constants' => [],
'expose-classes' => [],
'expose-functions' => [],

'exclude-namespaces' => [],
'exclude-constants' => [],
'exclude-classes' => [],
'exclude-functions' => [],

'expected-recorded-classes' => [],
'expected-recorded-functions' => [],
],

'FQCN attribute' => <<<'PHP'
<?php

namespace PhpScoper\Command;

#[\PhpScoper\Attribute\AsCommand(name: "main")]
class MainCommand {}

----
<?php

namespace Humbug\PhpScoper\Command;

#[\Humbug\PhpScoper\Attribute\AsCommand(name: "main")]
class MainCommand
{
}

PHP,

'imported attribute' => <<<'PHP'
<?php

namespace PhpScoper\Command;

use PhpScoper\Attribute\AsCommand;

#[AsCommand(name: "main")]
class MainCommand {}

----
<?php

namespace Humbug\PhpScoper\Command;

use Humbug\PhpScoper\Attribute\AsCommand;
#[AsCommand(name: "main")]
class MainCommand
{
}

PHP,
];
2 changes: 1 addition & 1 deletion specs/misc/first-class-callables.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function b() {}
new \Humbug\X\A(...);
$this->foo(...);
$this?->foo(...);
#[\X\A(...)]
#[A(...)]
function b()
{
}
Expand Down
2 changes: 2 additions & 0 deletions src/PhpParser/NodeVisitor/NameStmtPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Humbug\PhpScoper\PhpParser\UseStmtName;
use Humbug\PhpScoper\Symbol\EnrichedReflector;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Closure;
Expand Down Expand Up @@ -68,6 +69,7 @@ final class NameStmtPrefixer extends NodeVisitorAbstract
{
private const SUPPORTED_PARENT_NODE_CLASS_NAMES = [
Alias::class,
Attribute::class,
ArrowFunction::class,
Catch_::class,
ConstFetch::class,
Expand Down