Skip to content

Commit 027eefc

Browse files
authored
Fix the prefixing of returned string literals (#292)
Closes #291
1 parent c09b336 commit 027eefc

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

specs/string-literal/return.php.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Scalar literal returned',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
23+
'whitelist-global-functions' => true,
24+
'registered-classes' => [],
25+
'registered-functions' => [],
26+
],
27+
28+
'String argument' => <<<'PHP'
29+
<?php
30+
31+
function () {
32+
return 'Symfony\\Component\\Yaml\\Ya_1';
33+
};
34+
35+
function () {
36+
return '\\Symfony\\Component\\Yaml\\Ya_1';
37+
};
38+
39+
function () {
40+
return 'Humbug\\Symfony\\Component\\Yaml\\Ya_1';
41+
};
42+
43+
function () {
44+
return '\\Humbug\\Symfony\\Component\\Yaml\\Ya_1';
45+
};
46+
47+
function () {
48+
return 'Closure';
49+
};
50+
51+
function () {
52+
return 'usedAttributes';
53+
};
54+
55+
function () {
56+
return 'FOO';
57+
};
58+
59+
function () {
60+
return 'PHP_EOL';
61+
};
62+
63+
----
64+
<?php
65+
66+
namespace Humbug;
67+
68+
function () {
69+
return 'Humbug\\Symfony\\Component\\Yaml\\Ya_1';
70+
};
71+
function () {
72+
return 'Humbug\\Symfony\\Component\\Yaml\\Ya_1';
73+
};
74+
function () {
75+
return 'Humbug\\Symfony\\Component\\Yaml\\Ya_1';
76+
};
77+
function () {
78+
return 'Humbug\\Symfony\\Component\\Yaml\\Ya_1';
79+
};
80+
function () {
81+
return 'Closure';
82+
};
83+
function () {
84+
return 'usedAttributes';
85+
};
86+
function () {
87+
return 'FOO';
88+
};
89+
function () {
90+
return 'PHP_EOL';
91+
};
92+
93+
PHP
94+
,
95+
];

src/PhpParser/NodeVisitor/StringScalarPrefixer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use PhpParser\Node\Param;
2929
use PhpParser\Node\Scalar\String_;
3030
use PhpParser\Node\Stmt\PropertyProperty;
31+
use PhpParser\Node\Stmt\Return_;
3132
use PhpParser\NodeVisitorAbstract;
3233
use function array_key_exists;
3334
use function array_shift;
@@ -122,6 +123,7 @@ private function prefixStringScalar(String_ $string): String_
122123
|| $parentNode instanceof Param
123124
|| $parentNode instanceof Const_
124125
|| $parentNode instanceof PropertyProperty
126+
|| $parentNode instanceof Return_
125127
)
126128
) {
127129
return $string;

0 commit comments

Comments
 (0)