Skip to content

Commit

Permalink
Fix reviews
Browse files Browse the repository at this point in the history
Co-authored-by: Dariusz Rumiński <dariusz.ruminski@gmail.com>
  • Loading branch information
liquid207 and keradus committed Mar 9, 2022
1 parent e6c8569 commit 2e156d2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 59 deletions.
18 changes: 8 additions & 10 deletions doc/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,18 @@ List of Available Rules

`Source PhpCsFixer\\Fixer\\ControlStructure\\ControlStructureContinuationPositionFixer <./../src/Fixer/ControlStructure/ControlStructureContinuationPositionFixer.php>`_
- `create_from_format_call <./rules/function_notation/create_from_format_call.rst>`_
- `date_time_create_from_format_call <./rules/function_notation/date_time_create_from_format_call.rst>`_

The first argument of ``DateTime::createFromFormat`` method must start with ``!``.

Consider this code:
``DateTime::createFromFormat('Y-m-d', '2022-02-11')``.
What value will be return? '2022-01-11 00:00:00.0'? No,
actual return value has 'H:i:s' section like '2022-02-11 16:55:37.0'.
Change 'Y-m-d' to '!Y-m-d', return value will be '2022-01-11
00:00:00.0'.
So add ``!`` to format string will make return value more
intuitive.

`Source PhpCsFixer\\Fixer\\FunctionNotation\\CreateFromFormatCallFixer <./../src/Fixer/FunctionNotation/CreateFromFormatCallFixer.php>`_
``DateTime::createFromFormat('Y-m-d', '2022-02-11')``.
What value will be returned? '2022-01-11 00:00:00.0'? No, actual return
value has 'H:i:s' section like '2022-02-11 16:55:37.0'.
Change 'Y-m-d' to '!Y-m-d', return value will be '2022-01-11 00:00:00.0'.
So, adding ``!`` to format string will make return value more intuitive.

`Source PhpCsFixer\\Fixer\\FunctionNotation\\DateTimeCreateFromFormatCallFixer <./../src/Fixer/FunctionNotation/DateTimeCreateFromFormatCallFixer.php>`_
- `date_time_immutable <./rules/class_usage/date_time_immutable.rst>`_

Class ``DateTimeImmutable`` should be used instead of ``DateTime``.
Expand Down
31 changes: 0 additions & 31 deletions doc/rules/function_notation/create_from_format_call.rst

This file was deleted.

29 changes: 29 additions & 0 deletions doc/rules/function_notation/date_time_create_from_format_call.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
==========================================
Rule ``date_time_create_from_format_call``
==========================================

The first argument of ``DateTime::createFromFormat`` method must start with
``!``.

Description
-----------

Consider this code:
``DateTime::createFromFormat('Y-m-d', '2022-02-11')``.
What value will be returned? '2022-01-11 00:00:00.0'? No, actual return
value has 'H:i:s' section like '2022-02-11 16:55:37.0'.
Change 'Y-m-d' to '!Y-m-d', return value will be '2022-01-11 00:00:00.0'.
So, adding ``!`` to format string will make return value more intuitive.

Examples
--------

Example #1
~~~~~~~~~~

.. code-block:: diff
--- Original
+++ New
-<?php \DateTime::createFromFormat('Y-m-d', '2022-02-11');
+<?php \DateTime::createFromFormat('!Y-m-d', '2022-02-11');
2 changes: 1 addition & 1 deletion doc/rules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Function Notation
- `combine_nested_dirname <./function_notation/combine_nested_dirname.rst>`_ *(risky)*

Replace multiple nested calls of ``dirname`` by only one call with second ``$level`` parameter. Requires PHP >= 7.0.
- `create_from_format_call <./function_notation/create_from_format_call.rst>`_
- `date_time_create_from_format_call <./function_notation/date_time_create_from_format_call.rst>`_

The first argument of ``DateTime::createFromFormat`` method must start with ``!``.
- `fopen_flag_order <./function_notation/fopen_flag_order.rst>`_ *(risky)*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class CreateFromFormatCallFixer extends AbstractFixer
final class DateTimeCreateFromFormatCallFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
Expand All @@ -33,10 +33,10 @@ public function getDefinition(): FixerDefinitionInterface
new CodeSample("<?php \\DateTime::createFromFormat('Y-m-d', '2022-02-11');\n"),
],
"Consider this code:
`DateTime::createFromFormat('Y-m-d', '2022-02-11')`.
What value will be return? '2022-01-11 00:00:00.0'? No, actual return value has 'H:i:s' section like '2022-02-11 16:55:37.0'.
Change 'Y-m-d' to '!Y-m-d', return value will be '2022-01-11 00:00:00.0'.
So add `!` to format string will make return value more intuitive."
`DateTime::createFromFormat('Y-m-d', '2022-02-11')`.
What value will be returned? '2022-01-11 00:00:00.0'? No, actual return value has 'H:i:s' section like '2022-02-11 16:55:37.0'.
Change 'Y-m-d' to '!Y-m-d', return value will be '2022-01-11 00:00:00.0'.
So, adding `!` to format string will make return value more intuitive."
);
}

Expand All @@ -55,19 +55,19 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$functionNameIndex = $index + 1;
$functionNameIndex = $tokens->getNextMeaningfulToken($index);

if (!$tokens[$functionNameIndex]->equals([T_STRING, 'createFromFormat'], false)) {
continue;
}

if (!$tokens[$functionNameIndex + 1]->equals('(')) {
if (!$tokens[$tokens->getNextMeaningfulToken($functionNameIndex)]->equals('(')) {
continue;
}

$classNamePreviousIndex = $tokens->getTokenNotOfKindsSibling($functionNameIndex, -1, [T_DOUBLE_COLON, T_NS_SEPARATOR, T_STRING]);
$classNameIndex = $index - 1;
$className = $tokens->generatePartialCode($classNamePreviousIndex + 1, $classNameIndex);
$classNamePreviousIndex = $tokens->getTokenNotOfKindsSibling($index, -1, [T_NS_SEPARATOR, T_STRING, T_COMMENT]);
$classNameIndex = $tokens->getPrevMeaningfulToken($index);
$className = $tokens->generatePartialCode($tokens->getNextMeaningfulToken($classNamePreviousIndex), $classNameIndex);

foreach ($useDeclarations as $useDeclaration) {
if ($useDeclaration->getShortName() === $className) {
Expand All @@ -89,10 +89,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$formatArgumentIndex = array_values($arguments)[0];
$format = $tokens[$formatArgumentIndex]->getContent();
$formatArgumentIndex = $tokens->getNextMeaningfulToken($openIndex);
$formatToken = $tokens[$formatArgumentIndex];
$format = $formatToken->getContent();

if (!\in_array(substr($format, 0, 1), ['\'', '"'], true) || '!' === substr($format, 1, 1)) {
if (!$formatToken->isGivenKind([T_CONSTANT_ENCAPSED_STRING]) || !\in_array(substr($format, 0, 1), ['\'', '"'], true) || '!' === substr($format, 1, 1)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

/**
* @internal
* @covers \PhpCsFixer\Fixer\FunctionNotation\CreateFromFormatCallFixer
* @covers \PhpCsFixer\Fixer\FunctionNotation\DateTimeCreateFromFormatCallFixer
*/
final class CreateFromFormatCallFixerTest extends AbstractFixerTestCase
final class DateTimeCreateFromFormatCallFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
Expand Down Expand Up @@ -66,8 +66,29 @@ public function provideFixCases(): \Generator
];

yield [
'<?php \DateTime::createFromFormat(/* aaa */ "!Y-m-d", \'2022-02-11\');',
'<?php \DateTime::createFromFormat(/* aaa */ "Y-m-d", \'2022-02-11\');',
'<?php \DateTime::createFromFormat(/* aaa */ \'!Y-m-d\', \'2022-02-11\');',
'<?php \DateTime::createFromFormat(/* aaa */ \'Y-m-d\', \'2022-02-11\');',
];

yield [
'<?php /*1*//*2*/DateTime/*3*/::/*4*/createFromFormat/*5*/(/*6*/"!Y-m-d"/*7*/,/*8*/"2022-02-11"/*9*/)/*10*/ ?>',
'<?php /*1*//*2*/DateTime/*3*/::/*4*/createFromFormat/*5*/(/*6*/"Y-m-d"/*7*/,/*8*/"2022-02-11"/*9*/)/*10*/ ?>',
];

yield [
'<?php \DateTime::createFromFormat(\'Y-m-d\');',
];

yield [
'<?php \DateTime::createFromFormat($a, $b);',
];

yield [
'<?php \DateTime::createFromFormat(\'Y-m-d\', $b, $c);',
];

yield [
'<?php A\DateTime::createFromFormat(\'Y-m-d\', \'2022-02-11\');',
];
}
}

0 comments on commit 2e156d2

Please sign in to comment.