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 prefixing classes used in try/catch blocks #167

Merged
merged 1 commit into from
Feb 20, 2018
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
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