Skip to content

Commit ac6f757

Browse files
committed
Merge branch '2.15'
* 2.15: Command::execute() should always return an integer. Rename method Fix format of GitLab report Fix test failure tweaks Fix imports detected as used in namespaces
2 parents dd2ca62 + d39b456 commit ac6f757

File tree

7 files changed

+127
-30
lines changed

7 files changed

+127
-30
lines changed

src/Console/Command/DescribeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
106106
if ('@' === $name[0]) {
107107
$this->describeSet($output, $name);
108108

109-
return null;
109+
return 0;
110110
}
111111

112112
$this->describeRule($output, $name);
@@ -126,6 +126,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
126126
null === $alternative ? '' : ' Did you mean "'.$alternative.'"?'
127127
));
128128
}
129+
130+
return 0;
129131
}
130132

131133
/**

src/Console/Command/ReadmeCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,7 @@ static function (array $matches) {
287287
$footer = str_replace('%download.url%', $downloadUrl, $footer);
288288

289289
$output->write($header."\n".$help."\n".$footer);
290+
291+
return 0;
290292
}
291293
}

src/Console/Command/SelfUpdateCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
171171
rename($tempFilename, $localFilename);
172172

173173
$output->writeln(sprintf('<info>php-cs-fixer updated</info> (<comment>%s</comment>)', $remoteTag));
174+
175+
return 0;
174176
}
175177
}

src/Fixer/Import/NoUnusedImportsFixer.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function (NamespaceUseAnalysis $useDeclaration) use ($namespace) {
105105
}
106106

107107
foreach ($currentNamespaceUseDeclarations as $useDeclaration) {
108-
if (!$this->importIsUsed($tokens, $namespace, $usagesSearchIgnoredIndexes, $useDeclaration->getShortName())) {
108+
if (!$this->isImportUsed($tokens, $namespace, $usagesSearchIgnoredIndexes, $useDeclaration->getShortName())) {
109109
$this->removeUseDeclaration($tokens, $useDeclaration);
110110
}
111111
}
@@ -114,9 +114,18 @@ function (NamespaceUseAnalysis $useDeclaration) use ($namespace) {
114114
}
115115
}
116116

117-
private function importIsUsed(Tokens $tokens, NamespaceAnalysis $namespace, array $ignoredIndexes, $shortName)
117+
/**
118+
* @param Tokens $tokens
119+
* @param NamespaceAnalysis $namespace
120+
* @param array<int, int> $ignoredIndexes
121+
* @param string $shortName
122+
*
123+
* @return bool
124+
*/
125+
private function isImportUsed(Tokens $tokens, NamespaceAnalysis $namespace, array $ignoredIndexes, $shortName)
118126
{
119-
for ($index = $namespace->getScopeStartIndex(); $index <= $namespace->getScopeEndIndex(); ++$index) {
127+
$namespaceEndIndex = $namespace->getScopeEndIndex();
128+
for ($index = $namespace->getScopeStartIndex(); $index <= $namespaceEndIndex; ++$index) {
120129
if (isset($ignoredIndexes[$index])) {
121130
$index = $ignoredIndexes[$index];
122131

@@ -125,12 +134,23 @@ private function importIsUsed(Tokens $tokens, NamespaceAnalysis $namespace, arra
125134

126135
$token = $tokens[$index];
127136

128-
if (
129-
$token->isGivenKind(T_STRING)
130-
&& 0 === strcasecmp($shortName, $token->getContent())
131-
&& !$tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind([T_NS_SEPARATOR, T_CONST, T_OBJECT_OPERATOR])
132-
) {
133-
return true;
137+
if ($token->isGivenKind(T_STRING)) {
138+
$prevMeaningfulToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
139+
140+
if ($prevMeaningfulToken->isGivenKind(T_NAMESPACE)) {
141+
$index = $tokens->getNextTokenOfKind($index, [';', '{', [T_CLOSE_TAG]]);
142+
143+
continue;
144+
}
145+
146+
if (
147+
0 === strcasecmp($shortName, $token->getContent())
148+
&& !$prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_CONST, T_OBJECT_OPERATOR])
149+
) {
150+
return true;
151+
}
152+
153+
continue;
134154
}
135155

136156
if ($token->isComment() && Preg::match(

src/Report/GitlabReporter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/**
1818
* Generates a report according to gitlabs subset of codeclimate json files.
1919
*
20+
* @see https://github.com/codeclimate/spec/blob/master/SPEC.md#data-types
21+
*
2022
* @author Hans-Christian Otto <c.otto@suora.com>
2123
*
2224
* @internal
@@ -42,9 +44,13 @@ public function generate(ReportSummary $reportSummary)
4244
foreach ($change['appliedFixers'] as $fixerName) {
4345
$report[] = [
4446
'description' => $fixerName,
45-
'location.path' => $fileName,
4647
'fingerprint' => md5($fileName.$fixerName),
47-
'location.lines.begin' => 0, // line numbers are required in the format, but not available to reports
48+
'location' => [
49+
'path' => $fileName,
50+
'lines' => [
51+
'begin' => 0, // line numbers are required in the format, but not available to reports
52+
],
53+
],
4854
];
4955
}
5056
}

tests/Fixer/Import/NoUnusedImportsFixerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,47 @@ private function withComplexStringVariable()
10381038
use MyTrait;
10391039
}
10401040

1041+
EOF
1042+
],
1043+
'imported_name_is_part_of_namespace' => [
1044+
<<<'EOF'
1045+
<?php
1046+
1047+
namespace App\Foo;
1048+
1049+
1050+
class Baz
1051+
{
1052+
}
1053+
1054+
EOF
1055+
,
1056+
<<<'EOF'
1057+
<?php
1058+
1059+
namespace App\Foo;
1060+
1061+
use Foo\Bar\App;
1062+
1063+
class Baz
1064+
{
1065+
}
1066+
1067+
EOF
1068+
],
1069+
'imported_name_is_part_of_namespace with closing tag' => [
1070+
<<<'EOF'
1071+
<?php
1072+
namespace A\B {?>
1073+
<?php
1074+
require_once __DIR__.'/test2.php' ?>
1075+
<?php
1076+
use X\Z\Y
1077+
?>
1078+
<?php
1079+
$y = new Y() ?>
1080+
<?php
1081+
var_dump($y);}
10411082
EOF
10421083
],
10431084
];

tests/Report/GitlabReporterTest.php

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ protected function createSimpleReport()
4848
return <<<'JSON'
4949
[{
5050
"description": "some_fixer_name_here",
51-
"location.path": "someFile.php",
52-
"location.lines.begin": 0,
53-
"fingerprint": "ad098ea6ea7a28dd85dfcdfc9e2bded0"
51+
"fingerprint": "ad098ea6ea7a28dd85dfcdfc9e2bded0",
52+
"location": {
53+
"path": "someFile.php",
54+
"lines": {
55+
"begin": 0
56+
}
57+
}
5458
}]
5559
JSON;
5660
}
@@ -71,14 +75,22 @@ protected function createWithAppliedFixersReport()
7175
return <<<'JSON'
7276
[{
7377
"description": "some_fixer_name_here_1",
74-
"location.path": "someFile.php",
75-
"location.lines.begin": 0,
76-
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff"
78+
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff",
79+
"location": {
80+
"path": "someFile.php",
81+
"lines": {
82+
"begin": 0
83+
}
84+
}
7785
},{
7886
"description": "some_fixer_name_here_2",
79-
"location.path": "someFile.php",
80-
"location.lines.begin": 0,
81-
"fingerprint": "acad4672140c737a83c18d1474d84074"
87+
"fingerprint": "acad4672140c737a83c18d1474d84074",
88+
"location": {
89+
"path": "someFile.php",
90+
"lines": {
91+
"begin": 0
92+
}
93+
}
8294
}]
8395
JSON;
8496
}
@@ -99,19 +111,31 @@ protected function createComplexReport()
99111
return <<<'JSON'
100112
[{
101113
"description": "some_fixer_name_here_1",
102-
"location.path": "someFile.php",
103-
"location.lines.begin": 0,
104-
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff"
114+
"fingerprint": "b74e9385c8ae5b1f575c9c8226c7deff",
115+
"location": {
116+
"path": "someFile.php",
117+
"lines": {
118+
"begin": 0
119+
}
120+
}
105121
},{
106122
"description": "some_fixer_name_here_2",
107-
"location.path": "someFile.php",
108-
"location.lines.begin": 0,
109-
"fingerprint": "acad4672140c737a83c18d1474d84074"
123+
"fingerprint": "acad4672140c737a83c18d1474d84074",
124+
"location": {
125+
"path": "someFile.php",
126+
"lines": {
127+
"begin": 0
128+
}
129+
}
110130
},{
111131
"description": "another_fixer_name_here",
112-
"location.path": "anotherFile.php",
113-
"location.lines.begin": 0,
114-
"fingerprint": "30e86e533dac0f1b93bbc3a55c6908f8"
132+
"fingerprint": "30e86e533dac0f1b93bbc3a55c6908f8",
133+
"location": {
134+
"path": "anotherFile.php",
135+
"lines": {
136+
"begin": 0
137+
}
138+
}
115139
}]
116140
JSON;
117141
}

0 commit comments

Comments
 (0)