Skip to content

Return value parseTypes() is bad #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TrueType opened this issue Nov 29, 2021 · 7 comments
Closed

Return value parseTypes() is bad #141

TrueType opened this issue Nov 29, 2021 · 7 comments
Assignees
Labels

Comments

@TrueType
Copy link

TrueType commented Nov 29, 2021

phpDocumentor v1.5.1 on TYPO3v10.4.21 with PHPv7.4.19

Extbase Variable Dump

array(1 item)
   0 => phpDocumentor\Reflection\Types\Object_prototype object
      fqsen => privatephpDocumentor\Reflection\Fqsenprototype object

Extbase Variable Dump

array(1 item)
    => phpDocumentor\Reflection\Types\Array_prototype object
      valueType => protectedphpDocumentor\Reflection\Types\Mixed_prototype object
      keyType => protected NULL
      defaultKeyType => protectedphpDocumentor\Reflection\Types\Compoundprototype object


(1/1) TypeError

Return value of phpDocumentor\Reflection\TypeResolver::parseTypes() must implement interface phpDocumentor\Reflection\Type, null returned
in /var/webroot/vendor/phpdocumentor/type-resolver/src/TypeResolver.php line 320

                );
            }
        } elseif (count($types) === 1) {
            return $types[0];
        }

        if ($compoundToken === '|') {
            return new Compound(array_values($types));

at phpDocumentor\Reflection\TypeResolver->parseTypes()
in /vendor/phpdocumentor/type-resolver/src/TypeResolver.php line 172


        /** @var ArrayIterator<int, string|null> $tokenIterator */
        $tokenIterator = new ArrayIterator($tokens);

        return $this->parseTypes($tokenIterator, $context, self::PARSER_IN_COMPOUND);
    }

    /**
     * Analyse each tokens and creates types

at phpDocumentor\Reflection\TypeResolver->resolve()
in /vendor/phpdocumentor/reflection-docblock/src/DocBlock/Tags/Var_.php line 68

        $variableName = '';

        // if the first item that is encountered is not a variable; it is a type
        if ($firstPart && $firstPart[0] !== '$') {
            $type = $typeResolver->resolve($firstPart, $context);
        } else {
            // first part is not a type; we should prepend it to the parts array for further processing
            array_unshift($parts, $firstPart);
        }

at phpDocumentor\Reflection\DocBlock\Tags\Var_::create()
in /vendor/phpdocumentor/reflection-docblock/src/DocBlock/StandardTagFactory.php line 217

        try {
            $callable = [$handlerClassName, 'create'];
            Assert::isCallable($callable);
            /** @phpstan-var callable(string): ?Tag $callable */
            $tag = call_user_func_array($callable, $arguments);

            return $tag ?? InvalidTag::create($body, $name);
        } catch (InvalidArgumentException $e) {
            return InvalidTag::create($body, $name)->withError($e);
Index: src/TypeResolver.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/TypeResolver.php	(date 1615285713667)
+++ src/TypeResolver.php	(date 1615285713667)
@@ -217,7 +217,7 @@
             }
         } elseif (count($types) === 1) {
-            return $types[0];
+            return $types[0] ? $types[0] : array_values($types)[0];
         }

         if ($compoundToken === '|') {
@jaapio
Copy link
Member

jaapio commented Nov 30, 2021

Hi,

Thanks for reporting this. Could you please provide some more information about the type that should be resolved that results into a null value? It looks like you found a bug which I would like to cover with a test before we start fixing this problem.

@TrueType
Copy link
Author

TrueType commented Dec 1, 2021

oh well. Here is the full stacktrace - https://forge.typo3.org/issues/96148
Above you see the variable dump. The return $types[0] expects an Array. But it gets an object of class phpDocumentor\Reflection\Types\Array_prototype

@TrueType
Copy link
Author

TrueType commented Jan 3, 2022

@jaapio Hi. Could you please try to reproduce the behaviour with the following:

/**
 * @var []
 */
protected $somevar;

Changing it to

/**
 * @var array
 */
protected $somevar;

solved the problem for me.

@jaapio
Copy link
Member

jaapio commented Jan 3, 2022

Thanks, that reproduces this issue

@jaapio
Copy link
Member

jaapio commented Jan 4, 2022

I created a patch, It would be nice if somebody was able to help me test this to see if it works like you are expecting it to work.

The type resolver will now throw an exception, because @var [] is not allowed.

@jaapio
Copy link
Member

jaapio commented Jan 4, 2022

I released v1.6.0 containing this patch. Let me know if you have any problems with this patch :-)

@jaapio jaapio closed this as completed Jan 4, 2022
@TrueType
Copy link
Author

I tested where I originally got the problem (TYPO3v10/Extbase) and it's solved. thx. @jaapio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants