Skip to content

Commit 54afa91

Browse files
committed
fix - psr12
1 parent dba4d8e commit 54afa91

File tree

1 file changed

+66
-10
lines changed

1 file changed

+66
-10
lines changed

fmt.stub.php

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7546,7 +7546,18 @@ public function format($source)
75467546
$count = count($lines);
75477547

75487548
for ($idx = 0; $idx < $count - 1; ++$idx) {
7549-
if (false === strpos($lines[$idx], '?') || false === strpos($lines[$idx], ':')) {
7549+
$line = $lines[$idx];
7550+
$trimmed = ltrim($line);
7551+
7552+
if ('' === $trimmed || 0 === strpos($trimmed, '//') || 0 === strpos($trimmed, '#') || 0 === strpos($trimmed, '/*') || 0 === strpos($trimmed, '*')) {
7553+
continue;
7554+
}
7555+
7556+
if (false === strpos($line, '?') || false === strpos($line, ':')) {
7557+
continue;
7558+
}
7559+
7560+
if (preg_match('/["\"][^"\"]*\?[^"\"]*:[^"\"]*["\"]/u', $line)) {
75507561
continue;
75517562
}
75527563

@@ -7555,7 +7566,7 @@ public function format($source)
75557566
continue;
75567567
}
75577568

7558-
if (! preg_match('/^(\s*)(.+?)\s*\?\s*(.+?)\s*:\s*$/', $lines[$idx], $matches)) {
7569+
if (! preg_match('/^(\s*)(.+?)\s*\?\s*(.+?)\s*:\s*$/', $line, $matches)) {
75597570
continue;
75607571
}
75617572

@@ -7610,16 +7621,20 @@ public function format($source)
76107621
continue;
76117622
}
76127623

7613-
if (false === strpos($lines[$idx], '(') || substr_count($lines[$idx], '(') <= substr_count($lines[$idx], ')')) {
7624+
$parenPos = strrpos($lines[$idx], '(');
7625+
if (false === $parenPos) {
76147626
continue;
76157627
}
76167628

7617-
$baseIndent = substr($lines[$idx], 0, strlen($lines[$idx]) - strlen($trimmed));
7618-
$parenPos = strrpos($lines[$idx], '(');
7619-
if (false === $parenPos) {
7629+
if (! $this->isCallPrefix(substr($lines[$idx], 0, $parenPos))) {
7630+
continue;
7631+
}
7632+
7633+
if (substr_count($lines[$idx], '(') <= substr_count($lines[$idx], ')')) {
76207634
continue;
76217635
}
76227636

7637+
$baseIndent = substr($lines[$idx], 0, strlen($lines[$idx]) - strlen($trimmed));
76237638
$prefix = rtrim(substr($lines[$idx], 0, $parenPos));
76247639
$firstArg = trim(substr($lines[$idx], $parenPos + 1));
76257640

@@ -7634,8 +7649,7 @@ public function format($source)
76347649
continue;
76357650
}
76367651

7637-
$block = array_slice($lines, $idx, $endIdx - $idx + 1);
7638-
$lastLine = array_pop($block);
7652+
$lastLine = $lines[$endIdx];
76397653
$closePos = strpos($lastLine, ')');
76407654
if (false === $closePos) {
76417655
continue;
@@ -7696,6 +7710,16 @@ public function getExample()
76967710
);
76977711
EOT;
76987712
}
7713+
7714+
private function isCallPrefix($prefix)
7715+
{
7716+
$prefix = rtrim($prefix);
7717+
if ('' === $prefix) {
7718+
return false;
7719+
}
7720+
7721+
return 1 === preg_match('/(?:[A-Za-z_][A-Za-z0-9_]*|\$[A-Za-z_][A-Za-z0-9_]*|\]|\)|->\s*[A-Za-z_][A-Za-z0-9_]*|::\s*[A-Za-z_][A-Za-z0-9_]*)$/', $prefix);
7722+
}
76997723
}
77007724

77017725
final class PSR12ControlStructureBlocks extends AdditionalPass
@@ -7975,8 +7999,7 @@ public function candidate($source, $foundTokens)
79757999

79768000
public function format($source)
79778001
{
7978-
$source = preg_replace('/declare\s+\(/i', 'declare(', $source);
7979-
$source = preg_replace('/declare\s*\(\s*strict_types\s*=\s*1\s*\)\s*;(?!\s*:)/i', 'declare(strict_types=1);', $source);
8002+
$source = $this->normalizeDeclareStatements($source);
79808003
$source = (new OnlyOrderUseClauses())->format($source);
79818004

79828005
$tokens = token_get_all($source);
@@ -8162,6 +8185,39 @@ private function isSimpleDeclareStatement($tokens, $idx)
81628185

81638186
return false;
81648187
}
8188+
8189+
private function normalizeDeclareStatements($source)
8190+
{
8191+
$tokens = token_get_all($source);
8192+
$normalized = '';
8193+
$count = count($tokens);
8194+
8195+
for ($idx = 0; $idx < $count; ++$idx) {
8196+
list($id, $text) = $this->getToken($tokens[$idx]);
8197+
if (T_DECLARE !== $id) {
8198+
$normalized .= $text;
8199+
continue;
8200+
}
8201+
8202+
$statement = $text;
8203+
++$idx;
8204+
while ($idx < $count) {
8205+
list($subId, $subText) = $this->getToken($tokens[$idx]);
8206+
$statement .= $subText;
8207+
if (ST_SEMI_COLON === $subId || ST_COLON === $subId) {
8208+
break;
8209+
}
8210+
++$idx;
8211+
}
8212+
8213+
$statement = preg_replace('/^declare\s*\(/i', 'declare(', $statement);
8214+
$statement = preg_replace('/\(\s*strict_types\s*=\s*1\s*\)(\s*[;:])/i', '(strict_types=1)$1', $statement);
8215+
$statement = preg_replace('/\(\s*ticks\s*=\s*([0-9]+)\s*\)(\s*[;:])/i', '(ticks = $1)$2', $statement);
8216+
$normalized .= $statement;
8217+
}
8218+
8219+
return $normalized;
8220+
}
81658221
}
81668222

81678223
final class PSR12TraitUseSpacing extends AdditionalPass

0 commit comments

Comments
 (0)