Skip to content

Commit

Permalink
fix: Scope string literals used in expressions
Browse files Browse the repository at this point in the history
Closes humbug#557.
  • Loading branch information
theofidry committed May 7, 2024
1 parent c1e866e commit 487b1dd
Show file tree
Hide file tree
Showing 7 changed files with 957 additions and 429 deletions.
894 changes: 894 additions & 0 deletions specs/misc/composer.php

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions specs/string-literal/expr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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.
*/

use Humbug\PhpScoper\SpecFramework\Config\Meta;

return [
'meta' => new Meta(
title: 'Scalar literal used in an expression',
),

'String value used in a comparison expression' => <<<'PHP'
<?php
namespace Composer\Package\Loader;
if ($class !== 'Composer\Package\CompletePackage' && $class !== 'Composer\Package\RootPackage') {
trigger_error('The $class arg is deprecated, please reach out to Composer maintainers ASAP if you still need this.', E_USER_DEPRECATED);
}
----
<?php
namespace Humbug\Composer\Package\Loader;
if ($class !== 'Humbug\Composer\Package\CompletePackage' && $class !== 'Humbug\Composer\Package\RootPackage') {
trigger_error('The $class arg is deprecated, please reach out to Composer maintainers ASAP if you still need this.', \E_USER_DEPRECATED);
}

PHP,

'Non valid class name string value used in a comparison expression' => <<<'PHP'
<?php
namespace Composer\Package\Loader;
if ($class !== '1Composer\Package\CompletePackage' && $class !== '0Composer\Package\RootPackage') {
trigger_error('The $class arg is deprecated, please reach out to Composer maintainers ASAP if you still need this.', E_USER_DEPRECATED);
}
----
<?php
namespace Humbug\Composer\Package\Loader;
if ($class !== '1Composer\Package\CompletePackage' && $class !== '0Composer\Package\RootPackage') {
trigger_error('The $class arg is deprecated, please reach out to Composer maintainers ASAP if you still need this.', \E_USER_DEPRECATED);
}

PHP,
];
2 changes: 0 additions & 2 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

namespace Humbug\PhpScoper\Configuration;

use Humbug\PhpScoper\Patcher\ComposerPatcher;
use Humbug\PhpScoper\Patcher\Patcher;
use Humbug\PhpScoper\Patcher\PatcherChain;
use Humbug\PhpScoper\Patcher\SymfonyParentTraitPatcher;
Expand Down Expand Up @@ -89,7 +88,6 @@ public function create(?string $path = null, array $paths = []): Configuration

array_unshift($patchers, new SymfonyPatcher());
array_unshift($patchers, new SymfonyParentTraitPatcher());
array_unshift($patchers, new ComposerPatcher());

$symbolsConfiguration = $this->symbolsConfigurationFactory->createSymbolsConfiguration($config);

Expand Down
66 changes: 0 additions & 66 deletions src/Patcher/ComposerPatcher.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/PhpParser/NodeVisitor/StringScalarPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -174,6 +175,7 @@ private function prefixStringScalar(String_ $string): String_
$parentNode instanceof Assign
|| $parentNode instanceof Param
|| $parentNode instanceof Const_
|| $parentNode instanceof Expr
|| $parentNode instanceof PropertyProperty
|| $parentNode instanceof Return_
)) {
Expand Down
3 changes: 0 additions & 3 deletions tests/Configuration/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Fidry\FileSystem\FS;
use Humbug\PhpScoper\Container;
use Humbug\PhpScoper\FileSystemTestCase;
use Humbug\PhpScoper\Patcher\ComposerPatcher;
use Humbug\PhpScoper\Patcher\PatcherChain;
use Humbug\PhpScoper\Patcher\SymfonyParentTraitPatcher;
use Humbug\PhpScoper\Patcher\SymfonyPatcher;
Expand Down Expand Up @@ -60,7 +59,6 @@ public function test_it_can_be_created_without_a_file(): void
self::assertSame([], $configuration->getFilesWithContents());
self::assertEquals(
new PatcherChain([
new ComposerPatcher(),
new SymfonyParentTraitPatcher(),
new SymfonyPatcher(),
]),
Expand Down Expand Up @@ -141,7 +139,6 @@ public function test_it_can_create_a_complete_configuration(): void
);
self::assertEquals(
new PatcherChain([
new ComposerPatcher(),
new SymfonyParentTraitPatcher(),
new SymfonyPatcher(),
]),
Expand Down
Loading

0 comments on commit 487b1dd

Please sign in to comment.