phpDocumentor / ReflectionDocBlock

MIT License
9.35k stars 119 forks source link

Rendering a Description which contains a '%' sign leads to a vsprintf error #358

Closed andrewnicols closed 7 months ago

andrewnicols commented 10 months ago

I am writing a tool that inspects docblocks in one class, and copies them to another (an automatically generated Facade).

To do so I am using phpDocumentor\Reflection\DocBlockFactory to create a DocBlock from existing documentation, and then formatting it as a @method static doc on a class.

The code looks something like this, but I'm actually using the Serializer to format the new docs:


    /**
     * Method which contains a modulo sign (%) in its description.
     *
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
     * @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
     *
     * @covers ::__construct
     * @covers ::render
     * @covers ::__toString
     */
    public function testDescriptionCanContainPercent(): void
    {
        $factory = DocBlockFactory::createInstance();
        $contextFactory = new ContextFactory();

        $method = new \ReflectionMethod(self::class, 'testDescriptionCanContainPercent');

        $docblock = $factory->create(
            docblock: $method,
            context: $contextFactory->createFromReflector($method->getDeclaringClass()),
        );

        $tag1 = new Generic('JoinColumn', new Description('(name="column_id", referencedColumnName="id")'));
        $tag2 = new Generic('JoinColumn', new Description('(name="column_id_2", referencedColumnName="id")'));

        $tags = [
            $tag1,
            $tag2,
        ];

        $fixture  = new Description($docblock->getSummary(), $tags);
        $expected = 'Method which contains a modulo sign (%) in its description';
        $this->assertSame($expected, (string) $fixture);
    }

Running the above leads to:

1) phpDocumentor\Reflection\DocBlock\DescriptionTest::testDescriptionCanContainSpecialCharacters
ValueError: Unknown format specifier ")"

That's because the existing doc contains the % character, which vsprintf is expecting to treat as a placeholder.

jaapio commented 7 months ago

resolved in #357 thanks for reporting this