Skip to content

Commit

Permalink
Fix prefixing classes used in try/catch blocks (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Feb 20, 2018
1 parent 243d41c commit 5e59d9a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
156 changes: 156 additions & 0 deletions specs/catch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

declare(strict_types=1);

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

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

'Catch an internal class' => <<<'PHP'
<?php
try {
echo "foo";
} catch (Throwable $t) {
}
----
<?php
namespace Humbug;
try {
echo "foo";
} catch (\Throwable $t) {
}

PHP
,

'Catch an internal class in a namespace' => <<<'PHP'
<?php
namespace Acme;
try {
echo "foo";
} catch (\Throwable $t) {
}
----
<?php
namespace Humbug\Acme;
try {
echo "foo";
} catch (\Throwable $t) {
}

PHP
,

'Catch an custom exception class' => <<<'PHP'
<?php
try {
echo "foo";
} catch (FooException $t) {
}
----
<?php
namespace Humbug;
try {
echo "foo";
} catch (\Humbug\FooException $t) {
}

PHP
,

'Catch an custom exception class in a namespace' => <<<'PHP'
<?php
namespace Acme;
try {
echo "foo";
} catch (FooException $t) {
}
----
<?php
namespace Humbug\Acme;
try {
echo "foo";
} catch (\Humbug\Acme\FooException $t) {
}

PHP
,

'Catch an custom exception class in a namespace imported with a use statement' => <<<'PHP'
<?php
namespace Acme;
use X\FooException;
try {
echo "foo";
} catch (FooException $t) {
}
----
<?php
namespace Humbug\Acme;
use Humbug\X\FooException;
try {
echo "foo";
} catch (\Humbug\X\FooException $t) {
}

PHP
,

'Multiple catch statement' => <<<'PHP'
<?php
namespace Acme;
use X\FooException;
try {
echo "foo";
} catch (FooException | \Throwable $t) {
}
----
<?php
namespace Humbug\Acme;
use Humbug\X\FooException;
try {
echo "foo";
} catch (\Humbug\X\FooException|\Throwable $t) {
}

PHP
,
];
2 changes: 2 additions & 0 deletions src/NodeVisitor/NameStmtPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
Expand Down Expand Up @@ -106,6 +107,7 @@ private function prefixName(Name $name): Node
|| $parentNode instanceof New_
|| $parentNode instanceof Class_
|| $parentNode instanceof Interface_
|| $parentNode instanceof Catch_
)
) {
return $name;
Expand Down

0 comments on commit 5e59d9a

Please sign in to comment.