Verified Commit 5d783fbd authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3440170 by mxr576, quietone, smustgrave, longwave, larowlan: Forked...

Issue #3440170 by mxr576, quietone, smustgrave, longwave, larowlan: Forked DocParser incorrectly parses ::class notations

(cherry picked from commit f1f27ff7)
parent 398192b2
Loading
Loading
Loading
Loading
Loading
+28 −4
Original line number Diff line number Diff line
@@ -937,10 +937,16 @@ private function Constant()
            }
        }

        // checks if identifier ends with ::class, \strlen('::class') === 7
        $classPos = stripos($identifier, '::class');
        if ($classPos === strlen($identifier) - 7) {
            return substr($identifier, 0, $classPos);
        /**
         * Checks if identifier ends with ::class and remove the leading backslash if it exists.
         */
        if ($this->identifierEndsWithClassConstant($identifier) && ! $this->identifierStartsWithBackslash($identifier))
        {
            return substr($identifier, 0, $this->getClassConstantPositionInIdentifier($identifier));
        }
        if ($this->identifierEndsWithClassConstant($identifier) && $this->identifierStartsWithBackslash($identifier))
        {
            return substr($identifier, 1, $this->getClassConstantPositionInIdentifier($identifier) - 1);
        }

        if (!defined($identifier)) {
@@ -950,6 +956,24 @@ private function Constant()
        return constant($identifier);
    }

    private function identifierStartsWithBackslash(string $identifier) : bool
    {
        return '\\' === $identifier[0];
    }

    private function identifierEndsWithClassConstant(string $identifier) : bool
    {
        return $this->getClassConstantPositionInIdentifier($identifier) === strlen($identifier) - strlen('::class');
    }

    /**
     * @return int|false
     */
    private function getClassConstantPositionInIdentifier(string $identifier)
    {
        return stripos($identifier, '::class');
    }

    /**
     * Identifier ::= string
     *
+4 −0
Original line number Diff line number Diff line
@@ -764,6 +764,10 @@ public function getConstantsProvider()
            '@AnnotationWithConstants(PHP_EOL)',
            PHP_EOL
        );
        $provider[] = array(
            '@AnnotationWithConstants(\SimpleXMLElement::class)',
            \SimpleXMLElement::class
        );
        $provider[] = array(
            '@AnnotationWithConstants(AnnotationWithConstants::INTEGER)',
            AnnotationWithConstants::INTEGER