Skip to content

Commit e7471eb

Browse files
committed
Merge branch '2.15' into 2.16
* 2.15: HelpCommandTest - toString nested array Allow composer/semver ^2.0 and ^3.0 LinebreakAfterOpeningTagFixer - do not change code if linebreak already present
2 parents 7405b4d + 49397c2 commit e7471eb

File tree

5 files changed

+120
-35
lines changed

5 files changed

+120
-35
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"php": "^5.6 || ^7.0",
1818
"ext-json": "*",
1919
"ext-tokenizer": "*",
20-
"composer/semver": "^1.4",
20+
"composer/semver": "^1.4 || ^2.0 || ^3.0",
2121
"composer/xdebug-handler": "^1.2",
2222
"doctrine/annotations": "^1.2",
2323
"php-cs-fixer/diff": "^1.3",

src/Console/Command/HelpCommand.php

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -317,38 +317,10 @@ static function ($line) { return ' $ '.$line; },
317317
*/
318318
public static function toString($value)
319319
{
320-
if (\is_array($value)) {
321-
// Output modifications:
322-
// - remove new-lines
323-
// - combine multiple whitespaces
324-
// - switch array-syntax to short array-syntax
325-
// - remove whitespace at array opening
326-
// - remove trailing array comma and whitespace at array closing
327-
// - remove numeric array indexes
328-
static $replaces = [
329-
['#\r|\n#', '#\s{1,}#', '#array\s*\((.*)\)#s', '#\[\s+#', '#,\s*\]#', '#\d+\s*=>\s*#'],
330-
['', ' ', '[$1]', '[', ']', ''],
331-
];
332-
333-
$str = var_export($value, true);
334-
do {
335-
$strNew = Preg::replace(
336-
$replaces[0],
337-
$replaces[1],
338-
$str
339-
);
340-
341-
if ($strNew === $str) {
342-
break;
343-
}
344-
345-
$str = $strNew;
346-
} while (true);
347-
} else {
348-
$str = var_export($value, true);
349-
}
350-
351-
return Preg::replace('/\bNULL\b/', 'null', $str);
320+
return \is_array($value)
321+
? static::arrayToString($value)
322+
: static::scalarToString($value)
323+
;
352324
}
353325

354326
/**
@@ -634,4 +606,62 @@ private static function wordwrap($string, $width)
634606
return implode(' ', $line);
635607
}, $result);
636608
}
609+
610+
/**
611+
* @param mixed $value
612+
*
613+
* @return string
614+
*/
615+
private static function scalarToString($value)
616+
{
617+
$str = var_export($value, true);
618+
619+
return Preg::replace('/\bNULL\b/', 'null', $str);
620+
}
621+
622+
/**
623+
* @param mixed $value
624+
*
625+
* @return string
626+
*/
627+
private static function arrayToString(array $value)
628+
{
629+
if (0 === \count($value)) {
630+
return '[]';
631+
}
632+
633+
$isHash = static::isHash($value);
634+
$str = '[';
635+
636+
foreach ($value as $k => $v) {
637+
if ($isHash) {
638+
$str .= static::scalarToString($k).' => ';
639+
}
640+
641+
$str .= \is_array($v)
642+
? static::arrayToString($v).', '
643+
: static::scalarToString($v).', '
644+
;
645+
}
646+
647+
return substr($str, 0, -2).']';
648+
}
649+
650+
/**
651+
* @return bool
652+
*/
653+
private static function isHash(array $array)
654+
{
655+
$i = 0;
656+
657+
foreach ($array as $k => $v) {
658+
if ($k !== $i) {
659+
return true;
660+
}
661+
662+
++$i;
663+
}
664+
665+
return false;
666+
}
637667
}

src/Fixer/PhpTag/LinebreakAfterOpeningTagFixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
5353
return;
5454
}
5555

56+
// ignore if linebreak already present
57+
if (false !== strpos($tokens[0]->getContent(), "\n")) {
58+
return;
59+
}
60+
5661
$newlineFound = false;
5762
foreach ($tokens as $token) {
5863
if ($token->isWhitespace() && false !== strpos($token->getContent(), "\n")) {
@@ -67,7 +72,6 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
6772
return;
6873
}
6974

70-
$token = $tokens[0];
71-
$tokens[0] = new Token([$token->getId(), rtrim($token->getContent()).$this->whitespacesConfig->getLineEnding()]);
75+
$tokens[0] = new Token([T_OPEN_TAG, rtrim($tokens[0]->getContent()).$this->whitespacesConfig->getLineEnding()]);
7276
}
7377
}

tests/Console/Command/HelpCommandTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
use PhpCsFixer\Console\Application;
1616
use PhpCsFixer\Console\Command\HelpCommand;
17+
use PhpCsFixer\FixerConfiguration\FixerOption;
18+
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
1719
use PhpCsFixer\Tests\TestCase;
1820

1921
/**
@@ -23,6 +25,50 @@
2325
*/
2426
final class HelpCommandTest extends TestCase
2527
{
28+
/**
29+
* @param string $expected
30+
* @param mixed $input
31+
*
32+
* @dataProvider provideToStringCases
33+
*/
34+
public function testToString($expected, $input)
35+
{
36+
static::assertSame($expected, HelpCommand::toString($input));
37+
}
38+
39+
public function provideToStringCases()
40+
{
41+
yield ["['a' => 3, 'b' => 'c']", ['a' => 3, 'b' => 'c']];
42+
yield ['[[1], [2]]', [[1], [2]]];
43+
yield ['[0 => [1], \'a\' => [2]]', [[1], 'a' => [2]]];
44+
yield ['[1, 2, \'foo\', null]', [1, 2, 'foo', null]];
45+
yield ['[1, 2]', [1, 2]];
46+
yield ['[]', []];
47+
yield ['1.5', 1.5];
48+
yield ['false', false];
49+
yield ['true', true];
50+
yield ['1', 1];
51+
yield ["'foo'", 'foo'];
52+
}
53+
54+
/**
55+
* @param null|string $expected
56+
*
57+
* @dataProvider provideGetDisplayableAllowedValuesCases
58+
*/
59+
public function testGetDisplayableAllowedValues($expected, FixerOptionInterface $input)
60+
{
61+
static::assertSame($expected, HelpCommand::getDisplayableAllowedValues($input));
62+
}
63+
64+
public function provideGetDisplayableAllowedValuesCases()
65+
{
66+
yield [null, new FixerOption('foo', 'bar', false, null, ['int'], [])];
67+
yield [['A', 'B', 'x', 'z'], new FixerOption('foo', 'bar', false, null, ['string'], ['z', 'x', 'B', 'A'])];
68+
yield [[0, 3, 9], new FixerOption('foo', 'bar', false, null, ['int'], [0, 3, 9, static function () {}])];
69+
yield [null, new FixerOption('foo', 'bar')];
70+
}
71+
2672
public function testGetLatestReleaseVersionFromChangeLog()
2773
{
2874
$helpVersion = HelpCommand::getLatestReleaseVersionFromChangeLog();

tests/Fixer/PhpTag/LinebreakAfterOpeningTagFixerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public function provideFixCases()
8181
$foo = $bar;
8282
?>',
8383
],
84+
[
85+
str_replace("\n", "\r\n", '<?php
86+
// linebreak already present in file with Windows line endings
87+
'),
88+
],
8489
];
8590
}
8691

0 commit comments

Comments
 (0)