t3n / neos-debug

Adds a debug panel to your Neos CMS website
MIT License
30 stars 16 forks source link

Invalid return type in ContentCacheSegmentAspect #168

Closed MCStreetguy closed 2 years ago

MCStreetguy commented 2 years ago

Currently there is an error in the ContentCacheSegmentAspect when a cached segment results in a non-string value (e.g. through being nulled by an @if expression):

Return value of t3n\Neos\Debug\Aspect\ContentCacheSegmentAspect_Original::renderCacheInfoIntoSegment() must be of the type string, null returned

As far as I can tell, this is due to the fact, that the renderCacheInfoIntoSegment method declares a non-nullable return type of string, but may return non-string values, as the $segment variable also might be of any type, just like stated in the belonging docblock:

/**
 * @param mixed $segment This is mixed as the RuntimeContentCache might also return none string values
 * @param mixed[] $info
 */
protected function renderCacheInfoIntoSegment($segment, array $info): string
{
    // ...
    // Add debug data only to html output
    $segmentFormat = $info['entryIdentifier']['format'] ?? null;

    if ($segmentFormat !== 'html') {
        return $segment;
    }

    // ...

Therefore, in case of any non-string value such as integers, objects or like in my initial case null, this throws the above error. You can reproduce this behaviour with the following short fusion code i used for testing this bug:

prototype(Neos.Neos:Page) {
  body = Neos.Fusion:Value {
    value = ${null}
    //or: value = 25
    //or: value = ${node}

    @cache {
      mode = 'uncached'
      context {
        1 = 'node'
      }
    }
  }
}

The quickest solution I can think of would be to remove the return types alltogether as the return type can be nearly anything. I am happy to create an appropriate merge request, provided there is no disagreement with my proposed solution.

Sebobo commented 2 years ago

Your proposal sounds good, thx :)