Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: @PhpCsFixer ruleset - enable multiline_string_to_heredoc #7706

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions doc/ruleSets/PhpCsFixer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Rules

- `method_chaining_indentation <./../rules/whitespace/method_chaining_indentation.rst>`_
- `multiline_comment_opening_closing <./../rules/comment/multiline_comment_opening_closing.rst>`_
- `multiline_string_to_heredoc <./../rules/string_notation/multiline_string_to_heredoc.rst>`_
- `multiline_whitespace_before_semicolons <./../rules/semicolon/multiline_whitespace_before_semicolons.rst>`_ with config:

``['strategy' => 'new_line_for_chained_calls']``
Expand Down
8 changes: 8 additions & 0 deletions doc/rules/string_notation/multiline_string_to_heredoc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Example #2
+line1
+{$obj->getName()}
+EOD;

Rule sets
---------

The rule is part of the following rule set:

- `@PhpCsFixer <./../../ruleSets/PhpCsFixer.rst>`_

References
----------

Expand Down
264 changes: 136 additions & 128 deletions src/DocBlock/TypeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,143 +38,151 @@ final class TypeExpression
*
* @internal
*/
public const REGEX_TYPES = '(?<types>(?x) # one or several types separated by `|` or `&`
'.self::REGEX_TYPE.'
(?:
\h*(?<glue>[|&])\h*
(?&type)
)*+
)';

private const REGEX_TYPE = '(?<type>(?x) # single type
(?<nullable>\??\h*)
(?:
(?<array_shape>
(?<array_shape_start>(?i)(?:array|list|object)(?-i)\h*\{\h*)
(?<array_shape_inners>
(?<array_shape_inner>
(?<array_shape_inner_key>(?:(?&constant)|(?&identifier))\h*\??\h*:\h*|)
(?<array_shape_inner_value>(?&types_inner))
public const REGEX_TYPES = <<<'EOD'
(?<types>(?x) # one or several types separated by `|` or `&`

EOD.self::REGEX_TYPE.<<<'EOD'
mvorisek marked this conversation as resolved.
Show resolved Hide resolved

(?:
\h*(?<glue>[|&])\h*
(?&type)
)*+
)
EOD;

private const REGEX_TYPE = <<<'EOD'
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
(?<type>(?x) # single type
(?<nullable>\??\h*)
(?:
(?<array_shape>
(?<array_shape_start>(?i)(?:array|list|object)(?-i)\h*\{\h*)
(?<array_shape_inners>
(?<array_shape_inner>
(?<array_shape_inner_key>(?:(?&constant)|(?&identifier))\h*\??\h*:\h*|)
(?<array_shape_inner_value>(?&types_inner))
)
(?:
\h*,\h*
(?&array_shape_inner)
)*
(?:\h*,\h*)?
|)
\h*\}
)
(?:
\h*,\h*
(?&array_shape_inner)
)*
(?:\h*,\h*)?
|)
\h*\}
)
|
(?<callable> # callable syntax, e.g. `callable(string, int...): bool`
(?<callable_start>(?&name)\h*\(\h*)
(?<callable_arguments>
(?<callable_argument>
(?<callable_argument_type>(?&types_inner))
(?<callable_argument_is_reference>\h*&|)
(?<callable_argument_is_variadic>\h*\.\.\.|)
(?<callable_argument_name>\h*\$(?&identifier)|)
(?<callable_argument_is_optional>\h*=|)
|
(?<callable> # callable syntax, e.g. `callable(string, int...): bool`
(?<callable_start>(?&name)\h*\(\h*)
(?<callable_arguments>
(?<callable_argument>
(?<callable_argument_type>(?&types_inner))
(?<callable_argument_is_reference>\h*&|)
(?<callable_argument_is_variadic>\h*\.\.\.|)
(?<callable_argument_name>\h*\$(?&identifier)|)
(?<callable_argument_is_optional>\h*=|)
)
(?:
\h*,\h*
(?&callable_argument)
)*
(?:\h*,\h*)?
|)
\h*\)
(?:
\h*\:\h*
(?<callable_return>(?&type))
)?
)
(?:
\h*,\h*
(?&callable_argument)
)*
(?:\h*,\h*)?
|)
\h*\)
(?:
\h*\:\h*
(?<callable_return>(?&type))
)?
)
|
(?<generic> # generic syntax, e.g.: `array<int, \Foo\Bar>`
(?<generic_start>(?&name)\h*<\h*)
(?<generic_types>
(?&types_inner)
(?:
\h*,\h*
(?&types_inner)
)*
(?:\h*,\h*)?
)
\h*>
)
|
(?<class_constant> # class constants with optional wildcard, e.g.: `Foo::*`, `Foo::CONST_A`, `FOO::CONST_*`
(?&name)::\*?(?:(?&identifier)\*?)*
)
|
(?<constant> # single constant value (case insensitive), e.g.: 1, -1.8E+6, `\'a\'`
(?i)
# all sorts of numbers: with or without sign, supports literal separator and several numeric systems,
# e.g.: 1, +1.1, 1., .1, -1, 123E+8, 123_456_789, 0x7Fb4, 0b0110, 0o777
[+-]?(?:
(?:0b[01]++(?:_[01]++)*+)
| (?:0o[0-7]++(?:_[0-7]++)*+)
| (?:0x[\da-f]++(?:_[\da-f]++)*+)
| (?:(?<constant_digits>\d++(?:_\d++)*+)|(?=\.\d))
(?:\.(?&constant_digits)|(?<=\d)\.)?+
(?:e[+-]?(?&constant_digits))?+
)
| \'(?:[^\'\\\\]|\\\\.)*+\'
| "(?:[^"\\\\]|\\\\.)*+"
(?-i)
)
|
(?<this> # self reference, e.g.: $this, $self, @static
(?i)
[@$](?:this | self | static)
(?-i)
)
|
(?<name> # full name, e.g.: `int`, `\DateTime`, `\Foo\Bar`, `positive-int`
\\\\?+
(?<identifier>'.self::REGEX_IDENTIFIER.')
(?:[\\\\\-](?&identifier))*+
)
|
(?<parenthesized> # parenthesized type, e.g.: `(int)`, `(int|\stdClass)`
(?<parenthesized_start>
\(\h*
)
(?:
(?<parenthesized_types>
(?&types_inner)
|
(?<generic> # generic syntax, e.g.: `array<int, \Foo\Bar>`
(?<generic_start>(?&name)\h*<\h*)
(?<generic_types>
(?&types_inner)
(?:
\h*,\h*
(?&types_inner)
)*
(?:\h*,\h*)?
)
\h*>
)
|
(?<conditional> # conditional type, e.g.: `$foo is \Throwable ? false : $foo`
(?<conditional_cond_left>
(?:\$(?&identifier))
|
(?<conditional_cond_left_types>(?&types_inner))
(?<class_constant> # class constants with optional wildcard, e.g.: `Foo::*`, `Foo::CONST_A`, `FOO::CONST_*`
(?&name)::\*?(?:(?&identifier)\*?)*
)
|
(?<constant> # single constant value (case insensitive), e.g.: 1, -1.8E+6, `'a'`
(?i)
# all sorts of numbers: with or without sign, supports literal separator and several numeric systems,
# e.g.: 1, +1.1, 1., .1, -1, 123E+8, 123_456_789, 0x7Fb4, 0b0110, 0o777
[+-]?(?:
(?:0b[01]++(?:_[01]++)*+)
| (?:0o[0-7]++(?:_[0-7]++)*+)
| (?:0x[\da-f]++(?:_[\da-f]++)*+)
| (?:(?<constant_digits>\d++(?:_\d++)*+)|(?=\.\d))
(?:\.(?&constant_digits)|(?<=\d)\.)?+
(?:e[+-]?(?&constant_digits))?+
)
(?<conditional_cond_middle>
\h+(?i)is(?:\h+not)?(?-i)\h+
| '(?:[^'\\]|\\.)*+'
| "(?:[^"\\]|\\.)*+"
(?-i)
)
|
(?<this> # self reference, e.g.: $this, $self, @static
(?i)
[@$](?:this | self | static)
(?-i)
)
|
(?<name> # full name, e.g.: `int`, `\DateTime`, `\Foo\Bar`, `positive-int`
\\?+
(?<identifier>
EOD.self::REGEX_IDENTIFIER.<<<'EOD'
)
(?:[\\\-](?&identifier))*+
)
|
(?<parenthesized> # parenthesized type, e.g.: `(int)`, `(int|\stdClass)`
(?<parenthesized_start>
\(\h*
)
(?<conditional_cond_right_types>(?&types_inner))
(?<conditional_true_start>\h*\?\h*)
(?<conditional_true_types>(?&types_inner))
(?<conditional_false_start>\h*:\h*)
(?<conditional_false_types>(?&types_inner))
(?:
(?<parenthesized_types>
(?&types_inner)
)
|
(?<conditional> # conditional type, e.g.: `$foo is \Throwable ? false : $foo`
(?<conditional_cond_left>
(?:\$(?&identifier))
|
(?<conditional_cond_left_types>(?&types_inner))
)
(?<conditional_cond_middle>
\h+(?i)is(?:\h+not)?(?-i)\h+
)
(?<conditional_cond_right_types>(?&types_inner))
(?<conditional_true_start>\h*\?\h*)
(?<conditional_true_types>(?&types_inner))
(?<conditional_false_start>\h*:\h*)
(?<conditional_false_types>(?&types_inner))
)
)
\h*\)
)
)
\h*\)
)
)
(?<array> # array, e.g.: `string[]`, `array<int, string>[][]`
(\h*\[\h*\])*
)
(?:(?=1)0
(?<types_inner>
(?&type)
(?:
\h*[|&]\h*
(?&type)
)*+
(?<array> # array, e.g.: `string[]`, `array<int, string>[][]`
(\h*\[\h*\])*
)
(?:(?=1)0
(?<types_inner>
(?&type)
(?:
\h*[|&]\h*
(?&type)
)*+
)
|)
)
|)
)';
EOD;

private string $value;

Expand Down
30 changes: 16 additions & 14 deletions src/Fixer/Alias/MbStrFunctionsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,22 @@ public function getDefinition(): FixerDefinitionInterface
'Replace non multibyte-safe functions with corresponding mb function.',
[
new CodeSample(
'<?php
$a = strlen($a);
$a = strpos($a, $b);
$a = strrpos($a, $b);
$a = substr($a, $b);
$a = strtolower($a);
$a = strtoupper($a);
$a = stripos($a, $b);
$a = strripos($a, $b);
$a = strstr($a, $b);
$a = stristr($a, $b);
$a = strrchr($a, $b);
$a = substr_count($a, $b);
'
<<<'EOD'
<?php
$a = strlen($a);
$a = strpos($a, $b);
$a = strrpos($a, $b);
$a = substr($a, $b);
$a = strtolower($a);
$a = strtoupper($a);
$a = stripos($a, $b);
$a = strripos($a, $b);
$a = strstr($a, $b);
$a = stristr($a, $b);
$a = strrchr($a, $b);
$a = substr_count($a, $b);

EOD
),
],
null,
Expand Down
14 changes: 8 additions & 6 deletions src/Fixer/Alias/ModernizeStrposFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ public function getDefinition(): FixerDefinitionInterface
'Replace `strpos()` calls with `str_starts_with()` or `str_contains()` if possible.',
[
new CodeSample(
'<?php
if (strpos($haystack, $needle) === 0) {}
if (strpos($haystack, $needle) !== 0) {}
if (strpos($haystack, $needle) !== false) {}
if (strpos($haystack, $needle) === false) {}
'
<<<'EOD'
<?php
if (strpos($haystack, $needle) === 0) {}
if (strpos($haystack, $needle) !== 0) {}
if (strpos($haystack, $needle) !== false) {}
if (strpos($haystack, $needle) === false) {}

EOD
),
],
null,
Expand Down
Loading