Review main Abandoned

76747[WIP][BUGFIX] Improve type safety of GU explode methods

Oliver Klee CR 0 V 0 Easy +11 −5 updated 08/25/24, 10:10

Commit message

Also improve the type annotations and clean up some callers.

Also rename misleadingly-named a variable for more
consistency.

Run command:
> /Build/Scripts/runTests.sh -s phpstanGenerateBaseline

When backporting this patch, we need to undo this kind of change
(as `explode` can return `false' in PHP < 8):

```php
$result = explode($delimiter, $string) ?: [];
```

Review

  • Code review CR 0
  • Verified V 0

Issues 1

Files 5

Review log 2431

Show 56 earlier messages
  1. Oliver Klee uploaded patch set 1 11/20/22, 16:13

  2. Oliver Klee marked it work in progress 11/20/22, 16:14

  3. core-ci voted on patch set 1 V +1 11/20/22, 16:21 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/34012

  4. Oliver Klee uploaded patch set 2 V +1 11/20/22, 16:25

  5. Oliver Klee marked it ready for review 11/20/22, 16:26

  6. Oliver Klee uploaded patch set 3 11/20/22, 16:29

  1. core-ci voted on patch set 2 V +1 11/20/22, 16:35 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/34013

  2. core-ci voted on patch set 3 V +1 11/20/22, 16:38 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/34014

  3. Stefan Bürk commented on patch set 3 11/20/22, 17:36

    TransportFactory.php:90

    I would not remove this empty string check https://3v4l.org/ffSM3
  4. Oliver Klee commented on patch set 3 11/20/22, 17:44

    TransportFactory.php:90 Reply

    In line 79, trimExplode is called with the third argument $removeEmpty set to true. That means that there will be no empty string in the array. If we leave the check in, PHPStan will rightfully complain that this check will always be false.
  5. Stefan Bürk commented on patch set 3 11/20/22, 17:45

    TransportFactory.php:90 Reply

    Ack
  6. Nikita Hovratov voted on patch set 3 CR +1 11/22/22, 21:00

  7. Nikita Hovratov voted on patch set 3 V +1 11/22/22, 21:39

  8. Stefan Bürk voted on patch set 3 CR -1 11/22/22, 22:47

  9. Stefan Bürk commented on patch set 3 11/22/22, 22:59

    GeneralUtility.php:861

    This `(int)` cast here is definitly wrong. Result of "implode" *may* be a string with multiple values separated by `$delimiter`. For example: ```php $result = intExplode(',', '1,2,3,4,5', false, 3); // expected: [1,2,'3,4,5'] // with int cast: [1,2,3] ``` This is a absoluty change in behaviour. Granted, should be better documeted for the $limit argument. Also the return type annotation is wrong anyway. If behaviour should be changed, thenn there is no need to "implode" the "last elements" .. simply use the first one. Or shift the split (slicing) one position further and remove the last element handling. But this would be "breaking". We use this a lot in custom project extension, expecting exactly that behaviour. So maybe other developers are expecting the same. Thus not doable in v12, and even more not as backport. Sadly, there is no test for this case. But can be easily added. So, here should be rethought about what should be really done. ``` diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php index 205edf855a..c0adda0b6c 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php @@ -744,23 +744,33 @@ class GeneralUtilityTest extends UnitTestCase 'convertStringToInteger' => [ '1,foo,2', false, + 'limit' => 0, [1, 0, 2], ], 'zerosAreKept' => [ '0,1, 0, 2,0', false, + 'limit' => 0, [0, 1, 0, 2, 0], ], 'emptyValuesAreKept' => [ '0,1,, 0, 2,,0', false, + 'limit' => 0, [0, 1, 0, 0, 2, 0, 0], ], 'emptyValuesAreRemoved' => [ '0,1,, 0, 2,,0', true, + 'limit' => 0, [0=>0, 1=>1, 3=>0, 4=>2, 6=>0], // note does not renumber keys! ], + 'convertStringToIntegerKeepingOverLimit' => [ + '1,foo,2,3,4,5', + false, + 'limit' => 3, + [1, 0, '2,3,4,5'], + ], ]; } @@ -768,9 +778,9 @@ class GeneralUtilityTest extends UnitTestCase * @test * @dataProvider intExplodeDataProvider */ - public function intExplodeReturnsExplodedArray(string $input, bool $removeEmpty, array $expected): void + public function intExplodeReturnsExplodedArray(string $input, bool $removeEmpty, int $limit, array $expected): void { - self::assertSame($expected, GeneralUtility::intExplode(',', $input, $removeEmpty)); + self::assertSame($expected, GeneralUtility::intExplode(',', $input, $removeEmpty, $limit)); } ////////////////////////////////// ```
  10. Nikita Hovratov voted on patch set 3 V 0 CR 0 11/23/22, 16:34

  11. Oliver Klee commented on patch set 3 CR -1 11/23/22, 16:42

    Maybe I'll split this up into two changes. And we definitely should talk about the intExplode.
  12. Oliver Klee marked it work in progress 03/14/23, 15:16

  13. Oliver Klee uploaded patch set 4 CR -1 V +1 03/14/23, 15:17

  14. core-ci voted on patch set 4 V -1 03/14/23, 15:59 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/38935

  15. Oliver Klee uploaded patch set 5 V -1 03/27/23, 11:38

  16. Oliver Klee wrote 04/05/23, 12:02

    Topic set to phpstan
  17. Oliver Klee uploaded patch set 6 07/18/23, 15:41

  18. core-ci voted on patch set 6 V -1 07/18/23, 15:54 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/44269

  19. Oliver Klee rebased onto patch set 7 V -1 07/18/23, 22:01

  20. core-ci voted on patch set 7 V -1 07/18/23, 22:15 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/44284

  21. Oliver Klee uploaded patch set 8 V -1 11/25/23, 13:38

  22. core-ci voted on patch set 8 V +1 11/25/23, 13:44 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/49923

  23. Oliver Klee commented on patch set 8 11/25/23, 14:14

    This change is ready for review.

    GeneralUtility.php:861 Reply

    Closing this comment as now the change does not include the corresponding change in behavior anymore.
  24. Oliver Klee marked it work in progress 11/25/23, 14:14

  25. Oliver Klee abandoned the change 08/25/24, 10:10