Review main Abandoned

77896[TASK] Streamline TSFE::addCacheTags

Sybille Peters CR 0 V 0 Easy +21 −9 updated 02/24/23, 15:58

Commit message

Streamline adding cache tags in TSFE so that the existing function
addCacheTags is used and cache tags will only be added once.

Additionally a convenience function addCacheTag is added to add
only one cache tag.

Previously, addCacheTags used array_merge but not array_unique.
In another place in the core, array_merge and array_unique were used.
Theoretically, a cache tag could have been added multiple times.

Review

  • Code review CR 0
  • Verified V 0

Issues 1

Files 1

Review log 2836

Show 811 earlier messages
  1. Sybille Peters uploaded patch set 1 02/19/23, 10:44

  2. Sybille Peters updated the commit message in patch set 2 02/19/23, 10:47

  3. Sybille Peters uploaded patch set 3 02/19/23, 10:49

  4. Oliver Klee commented on patch set 3 02/19/23, 11:08

    Looks very good! I've added comments regarding making the new type annotation more specific.

    TypoScriptFrontendController.php:466

    We can be even more specific with list<string> (which means that the array keys are continuous int keys starting with 0).

    TypoScriptFrontendController.php:1941

    list<string>

    TypoScriptFrontendController.php:1960

    list<string>
  5. Sybille Peters commented on patch set 3 02/19/23, 11:21

    TypoScriptFrontendController.php:466 Reply

    Hmm, can we be sure this is always the case (with the continuous int)? In case an entry is removed in between, probably not. I think it should not be the case here but not 100% sure at this stage what array_merge and array_unique might do. So unless we are sure, I'd rather not change that. It would also help if I could run phpstan locally (which I cannot do currently, it hangs. I will try again and ask for help if not).
  6. Oliver Klee commented on patch set 3 02/19/23, 11:45

    TypoScriptFrontendController.php:466 Reply

    I could not find any code that removes any element from this array, only adding via on of these three ways: - `$this->pageCacheTags[] = …` - `$this->pageCacheTags = array_merge($this->pageCacheTags, …);` (just tested it: it will re-create the array keys even is `$this->pageCacheTags` is empty) - `$this->pageCacheTags = array_unique($this->pageCacheTags);` So we can be sure that `$this->pageCacheTags` will have 0-based, continuous int keys here.
  7. core-ci voted on patch set 3 V +1 02/19/23, 11:53 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37872

  8. core-ci voted on patch set 2 V +1 02/19/23, 11:54 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37871

  9. core-ci voted on patch set 1 V +1 02/19/23, 11:55 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37870

  10. Sybille Peters uploaded patch set 4 V +1 02/19/23, 13:26

  11. Sybille Peters commented on patch set 4 02/19/23, 13:26

    TypoScriptFrontendController.php:466 Reply

    Done

    TypoScriptFrontendController.php:1941 Reply

    Done

    TypoScriptFrontendController.php:1960 Reply

    Done
  1. core-ci voted on patch set 4 V +1 02/19/23, 13:33 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37877

  2. Oliver Klee commented on patch set 4 02/19/23, 14:05

    TypoScriptFrontendController.php:1954

    in_array in most cases should use the strict check: if (!in_array($tag, $this->pageCacheTags, true)) { (Sorry I missed this the first time.)
  3. Sybille Peters uploaded patch set 5 V +1 02/19/23, 15:58

  4. Sybille Peters commented on patch set 5 02/19/23, 15:59

    TypoScriptFrontendController.php:1954 Reply

    Thanks 😊
  5. Oliver Klee voted on patch set 5 CR +1 02/19/23, 16:02

  6. core-ci voted on patch set 5 V +1 02/19/23, 16:05 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37881

  7. Christian Futterlieb commented on patch set 5 02/20/23, 07:52

    TypoScriptFrontendController.php:466 Reply

    This solution does not enforce the numeric indexing from zero. If $tags contains an array with string keys for example, they would be preserved through array_merge and array_unique. The same applies to numeric indexed $tags arrays, that do not start with zero. Using array_values will do what you want: $this->pageCacheTags = array_merge($this->pageCacheTags, array_values($tags));
  8. Christian Futterlieb commented on patch set 5 02/20/23, 07:56

    TypoScriptFrontendController.php:466 Reply

    > The same applies to numeric indexed $tags arrays, that do not start with zero. This part is wrong, my bad. But the problem with the string-indexed $tags remains
  9. Sybille Peters commented on patch set 5 02/20/23, 08:47

    TypoScriptFrontendController.php:466 Reply

    @Christian Futterlieb What are you suggesting then? My take is : I would change the type back to string[]. But I am a bit at a loss what is current practice and would wait for other comments here. If this is called with an array with string keys, a warning will be issued via phpstan if an extension uses phpans and can be changed in the extension: TSFE->addCacheTags(array_value($tags)); For this patch it is not strictly necessary to enforce the stronger types via the PHPDocs. I do however think it is generally a good idea to have stronger types because it self-documents what values are expected and eases checks with phpstan. But, it is done as PHPDoc - so it won't break if a an array with string keys is passed. We could change it to array<int|string,string>, but I think that is worse than before because it implicitly suggest string keys are welcome here. For the functionality currently it is completely irrelevant what the keys are.
  10. Sybille Peters commented on patch set 5 02/20/23, 08:54

    TypoScriptFrontendController.php:466 Reply

    About the suggestion to use : $this->pageCacheTags = array_merge($this->pageCacheTags, array_values($tags)); in addCacheTags (if I understand it correctly): I don't think this is necessary or a good idea to do this in addCacheTags: both arrays are tagged as list<int> (or string [] before the last change) via PHPDoc - so at that point in the function I expect the type to (already) be correct. That would be the responsibility of the calling function and phpstan will call it out. If something should be array_values'd - it should be done BEFORE calling the function. However, if this is done anywhere in the core - we should change it back. But I expected phpstan would have could that. I hope my explanation is understandable and plausible.
  11. Oliver Klee commented on patch set 5 02/20/23, 15:58

    TypoScriptFrontendController.php:466 Reply

    Ah, I see. I indeed missed the case of $tags having string keys. Then the solution with array_values sounds like the right one to me, and addCacheTages should have one of these three @param annotations (all three are semantically identical): - string[] - array<string> - array<string|int, string>
  12. Sybille Peters uploaded patch set 6 CR +1 V +1 02/20/23, 16:12

  13. Sybille Peters commented on patch set 6 02/20/23, 16:17

    TypoScriptFrontendController.php:466 Reply

    Done
  14. core-ci voted on patch set 6 V +1 02/20/23, 16:18 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37902

  15. Sybille Peters uploaded patch set 7 V +1 02/20/23, 16:57

  16. core-ci voted on patch set 7 V +1 02/20/23, 17:03 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37903

  17. Sybille Peters commented on patch set 7 02/20/23, 17:06

    Oli and I talked: We now allow for array keys of int or string when accepting tags from "the outside" but internally, it is always converted into an int array. So in the class property $pageCacheTags and with getPageCacheTags we use list<string>.
  18. Oliver Klee voted on patch set 7 CR +1 02/20/23, 17:14

  19. Christian Futterlieb commented on patch set 7 02/20/23, 18:34

    Reply

    Great, that's exactly what I meant :)

    TypoScriptFrontendController.php:1945

    Since the discussion is about streamlining the type handling, it could be worth it to change this nested array-juggling to something like the following solution. To ensure not only the correct indexing, but also have a 'free' type-check on the values in the $tags array: foreach ($tags as $tag) { $this->addCacheTag($tag); } What do you think?
  20. Sybille Peters uploaded patch set 8 CR +1 V +1 02/21/23, 16:55

  21. Oliver Klee voted on patch set 8 CR +1 02/21/23, 16:57

  22. core-ci voted on patch set 8 V +1 02/21/23, 17:04 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/37936

  23. Christian Kuhn commented on patch set 8 CR -1 02/22/23, 10:36

    TypoScriptFrontendController.php:1945 Reply

    please no new public methods in tsfe: imho, the entire cache data stuff should be extracted to a request attribute object instead. if this is the way to go, adding new public methods to tsfe which deal with it, add headaches when following this path, so i'd love to avoid this.
  24. Sybille Peters commented on patch set 8 02/22/23, 11:31

    TypoScriptFrontendController.php:1945 Reply Unresolved

    Ok, but this patch does not add a new public function. It modifies an already existing public funtion addCacheTags and adds a protected function addCacheTag. That might be relevant for discussions about the other patch https://review.typo3.org/c/Packages/TYPO3.CMS/+/77894 But the latest variant with the event would avoid having to use public functions even for the cache tags.
  25. Sybille Peters abandoned the change 02/24/23, 15:58

    Refactoring planned "entire cache data should be extracted from tsfe and moved to a request attribute." so approach does not make that much sense now or should be postponed.