Skip to content

Commit

Permalink
Add support for Attributes (#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 9, 2022
1 parent 3ae8708 commit 16e4a86
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
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

0 comments on commit 16e4a86

Please sign in to comment.