Review main Merged

74378[TASK] Resolve "right side of && is always true" in PHPStan

crell CR +2 V +2 Normal +23 −66 updated 05/07/22, 16:53

Commit message

The PHPStan baseline file has several entries for this error.
This patch resolves them by adjusting the code so the
issue is not relevant.

Review

  • Code review CR +2 Done
    • Nikita Hovratov +2
    • Oliver Klee +1
  • Verified V +2 Done
    • Nikita Hovratov +2
    • Oliver Klee +1
    • core-ci +1 CI

Issues 1

Files 9

Review log 4351

Show 2226 earlier messages
  1. crell uploaded patch set 1 04/22/22, 23:01

  2. core-ci voted on patch set 1 V +1 04/22/22, 23:09 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26330

  3. Stefan Bürk commented on patch set 1 CR -1 04/23/22, 10:29

    -1 because of some changes in behaviour

    SelectTreeElement.php:96

    Are you aware that this drastically changes the behaviour and that your replacement fully drops the fallback to a default value ?

    LinkService.php:47

    We could think about using ``` array<string, LinkHandlingInterface> ``` here to be more precise - as we are working on this property.

    LinkService.php:110

    Change of behaviour. This will not match anylonger if ``` $urn = 'T3://' ``` Not sure if it is save to change the behaviour in this way here. Would suggest keep the stripos() variant for now.

    LazyLoadingProxy.php:46

    Should we really mix CPP with non promoted parameter handling ?

    Typo3DbQueryParser.php:727

    What's the benefit of this change ? It make the code not clearer, it do not fix anything. New code introduces more to think about and understand, which was not really needed in the old variant.
  4. Oliver Klee commented on patch set 1 04/24/22, 11:50

    Honestly, I'm not a big fan of mixing two things in one changeset (in this case: resolving a specific type of PHPStan warning and adding more native types). This makes review harder (as there is no clear focus/purpose for the changeset anymore), and this increases the impact of backing out the commit case it has broken something. In addition, this prevents us from backporting the cleanup to 11.5. So I propose to split this changeset into two changesets: a) fix the PHPStan warnings (non-breaking, backportable to 11.5) b) add the native type declarations (breaking, not backportable)
  5. Oliver Klee voted on patch set 1 CR -1 04/24/22, 11:51

  6. crell commented on patch set 1 04/25/22, 14:42

    Reply

    Not all of this is backportable, as the new code is often using PHP 8+ syntax. I wasn't planning on backporting these, as they're style fixes, not bug fixes, so no sense disrupting an LTS. Some of the type changes in this and the other patch are related to the PHPStan cleanup. It made sense to me to follow the campground rule (always leave it cleaner than you found it) and do the easy types that were adjacent to areas I was already messing with.

    SelectTreeElement.php:96 Reply

    Hrm. I confused $itemsToShow and $minItemsToShow. The names were too similar. :-/ Will adjust.

    LinkService.php:110 Reply

    That's what the strtolower() call is for. https://3v4l.org/TVse6 str_starts_with() has no case-insensitive variant precisely because using strtolower() like this is logically equivalent.

    LazyLoadingProxy.php:46 Reply

    I don't see an issue with it, personally. Especially when it's not all CPP only because makeIntance() doesn't work in the signature directly. (`new` calls do, and I've used that in the past as well.)

    Typo3DbQueryParser.php:727 Reply

    The old code was logically redundant, which PHPStan was objecting to. In particular, the final elseif clause I think is what was triggering it. ($includeDeleted is logically always true at that point, so redundant.) Early returns for error cases and fence posting is a very common technique, and works quite well here. Once I did that, my IDE just auto-recommended the rest. I actually had a really hard time following the nested conditionals in the original code. If the ($ignoreEnabledFields && !$includeDeleted) case were single line, I'd say the cleanest and most readable would be to switch to a multi-element match(). (cf: https://peakd.com/hive-168588/@crell/php-tricks-multi-value-match). If we can refactor that block to its own function, that would work well here, but I wasn't sure if that was too aggressive. If you think that's reasonable to do I can try the match() version, which should give the "lookup table" effect the old code was attempting to do, but IMO failing at.
  7. Oliver Klee commented on patch set 1 04/25/22, 14:58

    Reply

    Let me rephrase: I'll gladly review the two changesets if they're separated like I suggested, and I'm not willing to review if both things are mixed together (but I won't stand in your way in that case).
  8. crell uploaded patch set 2 04/25/22, 15:11

  9. crell commented on patch set 2 04/25/22, 15:11

    Reply

    Ack

    SelectTreeElement.php:96 Reply

    Done

    LinkService.php:47 Reply

    Done
  10. core-ci voted on patch set 2 V +1 04/25/22, 15:24 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26404

  11. Nikita Hovratov commented on patch set 2 CR -1 04/25/22, 15:55

    SelectTreeElement.php:88

    The int casting is missing here now.

    LazyLoadingProxy.php:56

    Are you sure always an object is returned here? The comment states "hopefully". Also the __call method does not check for object without a reason. This needs to be carefully verified, that this is not a problem anymore.

    Typo3DbQueryParser.php:727 Reply

    The refactoring looks fine to me, although I would prefer a separate patch to keep reviews as easy as possible. But you don't need to change it now.
  12. Stefan Bürk commented on patch set 2 CR -1 04/25/22, 17:02

    SelectTreeElement.php:55

    Extensions / project extensions may extend SelectTreeElement as base for custom select render type. Also this properpy can be set per tca configuration, they may have for e.g. "protected $itemsToShow = 30;" set - which will be hard breaking now. See: https://3v4l.org/Sprt9 So patch has to be marked as breaking, and include or amend to a corresponding breaking ReST.

    SelectTreeElement.php:61

    Extensions / project extensions may extend SelectTreeElement as base for custom select render type. This is breaking if they have overriden this property without type hint, e.g. "protected $minItemsToShow = 10;". See: https://3v4l.org/Sprt9 So patch has to be marked as breaking, and include or amend to a corresponding breaking ReST.

    SelectTreeElement.php:66

    Same as the two property above. Breaking.

    Typo3DbQueryParser.php:727 Reply

    Done
  13. crell commented on patch set 2 04/26/22, 18:23

    SelectTreeElement.php:55 Reply

    Do you think these should go in the same type-changes RST as we're already building up, or a separate one for each class, or...? I'm flexible here.

    SelectTreeElement.php:88 Reply

    Ugh. Looks like max() takes mixed args, but will then return the value in the same type that it received it rather than converting to a number. What fresh hell is this, PHP? I will adjust.

    LazyLoadingProxy.php:56 Reply

    I checked all places this method is called from and the places it calls; the current logic is horribly inconsistent; some places check the return value with is_object(), most don't. The two methods it calls to get its return value are both `return mixed`. So... honestly the docblock is just wrong here, or rather "hopefully" is carrying a lot of weight. :-) For simplicity I'll back out this change for now. We'll fry this fish later.
  14. crell uploaded patch set 3 04/26/22, 20:29

  15. core-ci voted on patch set 3 V +1 04/26/22, 20:42 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26478

  16. Benni Mack commented on patch set 3 04/27/22, 10:21

    LinkService.php:49

    I think we should initialize with an empty array? otherwise $this->handlers isn't for sure an array as the constructor _might_ populate it, but doesn't for sure populate it.
  17. Oli Bartsch commented on patch set 3 04/27/22, 10:54

    SelectTreeElement.php:88

    That's a change in behaviour, see https://3v4l.org/Un06M

    LinkService.php:134

    isset seems to be a change in behaviour, since the value could still be NULL. IMO this should be "$this->handlers[self::TYPE_EMAIL] ?? false" instead.

    LazyLoadingProxy.php:42

    If I remember correct, we agreed to only use property promotion when the full argument block can use it. Additionally I'm missing the property descriptions now, which were given in the corresponding PHPdoc's.

    LazyLoadingProxy.php:93

    This will fail in case $this->_loadRealInstance() does not return an object. The corresponding check IMO needs to be keep.
  18. Stefan Bürk commented on patch set 3 04/27/22, 14:04

    LinkService.php:134 Reply

    isset() returns false for null, so not really a change in behaviour. However ?? is broader known for "not set or null" than "isset()"
  19. Stefan Bürk commented on patch set 3 04/27/22, 14:05

    LinkService.php:134 Reply

  20. Oli Bartsch commented on patch set 3 04/27/22, 15:54

    LinkService.php:134 Reply

    Yes you are right.
  21. Stefan Bürk commented on patch set 3 04/27/22, 16:05

    SelectTreeElement.php:55 Reply

    > So patch has to be marked as breaking, and include or amend to a corresponding breaking ReST. ... include (a new file) or amend a existing one ...
  22. crell commented on patch set 3 04/27/22, 16:24

    SelectTreeElement.php:55 Reply

    Oh, I was smart and mentioned properties in the title there after all. :-) It looks like it can be added there without colliding with any other in-flight patches, so will do so directly.

    SelectTreeElement.php:61 Reply

    Done

    SelectTreeElement.php:66 Reply

    Done

    SelectTreeElement.php:88 Reply

    Sigh. Who in their right mind sets size to a negative number??? :-P

    LinkService.php:49 Reply

    Done

    LinkService.php:134 Reply

    I was trying to avoid issues with the priority order of ?? being so low, but it ends up being the same number of parentheses either way, so meh.

    LazyLoadingProxy.php:42 Reply

    Is that documented anywhere? I know it's been discussed around channel somewhat, but I don't recall a formal decision being made. I see no reason to triple the work/LOC because a single property needs one extra line of default handling. Also, the descriptions are same (give or take wording) as what's in the constructor docblock; I believe PHPStorm is smart enough to connect the two as being the same. If any other tools aren't, that's a bug in those tools that should be corrected.

    LazyLoadingProxy.php:93 Reply

    Leftover from when it seemed _loadRealInstance() was safe to type as :object. Will adjust.
  23. crell uploaded patch set 4 04/27/22, 16:24

  24. Stefan Bürk commented on patch set 4 04/27/22, 16:25

    SelectTreeElement.php:55 Reply

    wrong. I added and changed that for the property in the other patch.
  25. core-ci voted on patch set 4 V +1 04/27/22, 16:37 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26529

  1. Oliver Klee commented on patch set 4 04/27/22, 16:42

    SelectTreeElement.php:88

    Generally no conditions in conditions, please (with the exception of loops over DB results, where this is very hard to avoid). Instead, please let's put assign the variable first and have the condition in the next statement.
  2. crell commented on patch set 4 04/27/22, 17:06

    SelectTreeElement.php:88 Reply

    Ack
  3. crell uploaded patch set 5 04/27/22, 17:06

  4. core-ci voted on patch set 5 V +1 04/27/22, 17:15 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26531

  5. Oliver Hader commented on patch set 5 CR -1 05/02/22, 15:28

    SelectTreeElement.php:82

    Would `!empty($config['readOnly'])` have been enough? Now `$readOnly` is of type mixed oder false, previously it has been a boolean

    BcryptPasswordHash.php:78

    So, PASSWORD_BCRYPT can be empty now?

    MemorySpool.php:97

    This seems to be wrong, and would unshift the null-message

    LazyLoadingProxy.php:46

    What about using something like this? ``` ... private ?DataMapper $dataMapper = null, ) { $this->dataMapper ??= GeneralUtility::makeInstance(DataMapper::class); } ```

    LazyLoadingProxy.php:93

    superfluous space

    LazyLoadingProxy.php:157

    What about implementing `Stringable` interface?
  6. crell commented on patch set 5 05/02/22, 17:01

    SelectTreeElement.php:82 Reply

    I think you are correct. Will adjust.

    BcryptPasswordHash.php:78 Reply

    PASSWORD_BCRYPT is either not defined (thus the defined() call will already return false) or has the value `2y`, which is truthy. Thus the truthiness check is redundant.

    MemorySpool.php:97 Reply

    The exception cannot be thrown by `$message = array_pop(...)`. If $message is not a real value after that statement, the `while()` will terminate on its own. The only place that exception could be thrown is `$transport->send()`, which will only be reached if `$message` is truthy. Thus `$message` is always truthy on this line, and the check is redundant.

    LazyLoadingProxy.php:46 Reply

    That would *technically* make it type-legal for $this->dataMapper to have a null value, even if it wouldn't happen in practice. I just tested and it looks like neither phpstan nor PHPStorm complain about the potential null value, so either they're particularly smart or particularly dumb. 😊 Either way, I can roll with that so will change.

    LazyLoadingProxy.php:93 Reply

    Ack

    LazyLoadingProxy.php:157 Reply

    Ack
  7. crell uploaded patch set 6 05/02/22, 17:02

  8. Oliver Klee wrote 05/02/22, 17:08

    Removed reviewer Oliver Klee.
  9. core-ci voted on patch set 6 V +1 05/02/22, 17:13 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26740

  10. Oliver Hader wrote 05/06/22, 21:39

    Removed reviewer Oliver Hader.
  11. Nikita Hovratov commented on patch set 6 05/07/22, 12:37

    Honestly, let's split this patch into the non-breaking phpstan stuff and the breaking changed method signature stuff.
  12. Stefan Bürk wrote 05/07/22, 12:38

    Removed reviewer Stefan Bürk.
  13. Oliver Klee commented on patch set 6 05/07/22, 12:48

    Reply

    For that case, my offer of reviewing the changes still stands.
  14. Oliver Klee wrote 05/07/22, 12:48

    Removed cc Oliver Klee.
  15. Nikita Hovratov uploaded patch set 7 05/07/22, 16:33

  16. Nikita Hovratov updated the commit message in patch set 8 05/07/22, 16:33

  17. Oliver Klee voted on patch set 8 CR +1 05/07/22, 16:39

  18. Oliver Klee updated the commit message in patch set 9 05/07/22, 16:41

  19. Oliver Klee voted on patch set 9 CR +1 05/07/22, 16:41

  20. core-ci voted on patch set 8 V +1 05/07/22, 16:41 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26966

  21. Nikita Hovratov voted on patch set 9 V +1 CR +1 05/07/22, 16:45

  22. core-ci voted on patch set 9 V +1 05/07/22, 16:48 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/26967

  23. Oliver Klee voted on patch set 9 V +1 05/07/22, 16:49

  24. Nikita Hovratov voted on patch set 9 V +2 CR +2 05/07/22, 16:53

  25. Nikita Hovratov merged patch set 10 05/07/22, 16:53