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

Prefix instanceof statements #168

Merged
merged 3 commits 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
47 changes: 47 additions & 0 deletions specs/exp/cast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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' => 'Miscellaneous',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
'whitelist' => [],
],

'Cast variable' => <<<'PHP'
<?php

$x = new stdClass();

(bool) $x;
(int) $x;
(float) $x;
(array) $x;
(object) $x;
----
<?php

namespace Humbug;

$x = new \stdClass();
(bool) $x;
(int) $x;
(double) $x;
(array) $x;
(object) $x;

PHP
,
];
24 changes: 23 additions & 1 deletion specs/catch.php → specs/exp/catch.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

return [
'meta' => [
'title' => 'Miscellaneous',
'title' => 'Catch expressions',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
'whitelist' => [],
Expand Down Expand Up @@ -151,6 +151,28 @@
} catch (\Humbug\X\FooException|\Throwable $t) {
}

PHP
,

'catch with special keywords' => <<<'PHP'
<?php

namespace Acme;

try {
echo "foo";
} catch (self | parent $t) {
}
----
<?php

namespace Humbug\Acme;

try {
echo "foo";
} catch (self|parent $t) {
}

PHP
,
];
137 changes: 137 additions & 0 deletions specs/exp/instanceof.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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' => 'Instanceof expressions',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
'whitelist' => [],
],

'Instance of an internal class' => <<<'PHP'
<?php

$x = new stdClass();
$x instanceof stdClass;
----
<?php

namespace Humbug;

$x = new \stdClass();
$x instanceof \stdClass;

PHP
,

'Instance of an internal class in a namespace' => <<<'PHP'
<?php

namespace Acme;

use stdClass;

$x = new stdClass();
$x instanceof stdClass;

----
<?php

namespace Humbug\Acme;

use stdClass;
$x = new \stdClass();
$x instanceof \stdClass;

PHP
,

'Instance of a custom exception class' => <<<'PHP'
<?php

$x = new Foo();
$x instanceof Foo;

----
<?php

namespace Humbug;

$x = new \Humbug\Foo();
$x instanceof \Humbug\Foo;

PHP
,

'Instance of a custom exception class in a namespace' => <<<'PHP'
<?php

namespace Acme;

$x = new Foo();
$x instanceof Foo;

----
<?php

namespace Humbug\Acme;

$x = new \Humbug\Acme\Foo();
$x instanceof \Humbug\Acme\Foo;

PHP
,

'Instance of with ternary' => <<<'PHP'
<?php

namespace Acme;

$file = new \stdClass();

$file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);

----
<?php

namespace Humbug\Acme;

$file = new \stdClass();
$file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);

PHP
,

'Instance of with special keyword' => <<<'PHP'
<?php

namespace Acme;

$file instanceof static;
$file instanceof self;
$file instanceof parent;

----
<?php

namespace Humbug\Acme;

$file instanceof static;
$file instanceof self;
$file instanceof parent;

PHP
,
];
15 changes: 8 additions & 7 deletions src/Console/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
use InvalidArgumentException;
use Iterator;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use function Humbug\PhpScoper\iterables_to_iterator;
use function Humbug\PhpScoper\chain;

final class Configuration
{
Expand Down Expand Up @@ -70,7 +71,7 @@ public static function load(string $path = null, array $paths = []): self

$finders = self::retrieveFinders($config);
$filesFromPaths = self::retrieveFilesFromPaths($paths);
$filesWithContents = self::retrieveFilesWithContents(iterables_to_iterator($filesFromPaths, ...$finders));
$filesWithContents = self::retrieveFilesWithContents(chain($filesFromPaths, ...$finders));

return new self($path, $filesWithContents, $patchers, $whitelist);
}
Expand Down Expand Up @@ -100,7 +101,7 @@ private function __construct(
public function withPaths(array $paths): self
{
$filesWithContents = self::retrieveFilesWithContents(
iterables_to_iterator(
chain(
self::retrieveFilesFromPaths(
array_unique($paths)
)
Expand Down Expand Up @@ -310,14 +311,14 @@ private static function retrieveFilesWithContents(Iterator $files): array
{
return array_reduce(
iterator_to_array($files),
function (array $files, $fileInfo): array {
$file = (string) $fileInfo;
function (array $files, SplFileInfo $fileInfo): array {
$file = $fileInfo->getRealPath();

if (false === file_exists($file)) {
if (false === $file) {
throw new RuntimeException(
sprintf(
'Could not find the file "%s".',
$file
(string) $fileInfo
)
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/NodeVisitor/NameStmtPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
Expand All @@ -32,6 +33,7 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\NodeVisitorAbstract;
use function in_array;

/**
* ```
Expand Down Expand Up @@ -108,6 +110,7 @@ private function prefixName(Name $name): Node
|| $parentNode instanceof Class_
|| $parentNode instanceof Interface_
|| $parentNode instanceof Catch_
|| $parentNode instanceof Instanceof_
)
) {
return $name;
Expand All @@ -120,6 +123,8 @@ private function prefixName(Name $name): Node
|| $parentNode instanceof ClassConstFetch
|| $parentNode instanceof New_
|| $parentNode instanceof Param
|| $parentNode instanceof Catch_
|| $parentNode instanceof Instanceof_
)
&& in_array((string) $name, self::PHP_FUNCTION_KEYWORDS, true)
) {
Expand Down
18 changes: 3 additions & 15 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

namespace Humbug\PhpScoper;

use AppendIterator;
use ArrayIterator;
use Humbug\PhpScoper\Console\Application;
use Humbug\PhpScoper\Console\Command\AddPrefixCommand;
use Humbug\PhpScoper\Console\Command\InitCommand;
Expand All @@ -29,7 +27,6 @@
use Humbug\SelfUpdate\Exception\RuntimeException as SelfUpdateRuntimeException;
use Humbug\SelfUpdate\Updater;
use Iterator;
use IteratorAggregate;
use PackageVersions\Versions;
use PhpParser\Node;
use PhpParser\Parser;
Expand Down Expand Up @@ -211,20 +208,11 @@ function deep_clone($node)
return unserialize(serialize($node));
}

function iterables_to_iterator(iterable ...$iterables): Iterator
function chain(iterable ...$iterables): Iterator
{
$iterator = new AppendIterator();

foreach ($iterables as $iterable) {
if (is_array($iterable)) {
$iterator->append(new ArrayIterator($iterable));
} elseif ($iterable instanceof IteratorAggregate) {
$iterator->append($iterable->getIterator());
} else {
/* @var Iterator $iterable */
$iterator->append($iterable);
foreach ($iterable as $key => $value) {
yield $key => $value;
}
}

return $iterator;
}