Skip to content

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

@andrewnicols

Description

@andrewnicols

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions