File: InterpretingDocBlocksTest.php

package info (click to toggle)
php-phpdocumentor-reflection-docblock 5.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,416 kB
  • sloc: php: 9,035; xml: 67; makefile: 54
file content (487 lines) | stat: -rw-r--r-- 18,057 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php

declare(strict_types=1);

/**
 * This file is part of phpDocumentor.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @link      http://phpdoc.org
 */

namespace phpDocumentor\Reflection;

use Mockery as m;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\StandardTagFactory;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Method;
use phpDocumentor\Reflection\DocBlock\Tags\MethodParameter;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
use phpDocumentor\Reflection\DocBlock\Tags\See;
use phpDocumentor\Reflection\DocBlock\Tags\Since;
use phpDocumentor\Reflection\DocBlock\Tags\Template;
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Mixed_;
use phpDocumentor\Reflection\Types\Object_;
use phpDocumentor\Reflection\Types\Self_;
use phpDocumentor\Reflection\Types\String_;
use phpDocumentor\Reflection\Types\Void_;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

/**
 * @coversNothing
 */
class InterpretingDocBlocksTest extends TestCase
{
    /**
     * Call Mockery::close after each test.
     */
    public function tearDown(): void
    {
        m::close();
    }

    public function testInterpretingSummaryWithEllipsis(): void
    {
        $docblock = <<<DOCBLOCK
/**
 * This is a short (...) description.
 *
 * This is a long description.
 *
 * @return void
 */
DOCBLOCK;

        $factory = DocBlockFactory::createInstance();
        $phpdoc = $factory->create($docblock);

        $summary = 'This is a short (...) description.';
        $description = 'This is a long description.';

        $this->assertInstanceOf(DocBlock::class, $phpdoc);
        $this->assertSame($summary, $phpdoc->getSummary());
        $this->assertSame($description, $phpdoc->getDescription()->render());
        $this->assertCount(1, $phpdoc->getTags());
        $this->assertTrue($phpdoc->hasTag('return'));
    }

    public function testInterpretingASimpleDocBlock(): void
    {
        /** @var DocBlock $docblock */
        $docblock;
        /** @var string $summary */
        $summary;
        /** @var Description $description */
        $description;

        include(__DIR__ . '/../../examples/01-interpreting-a-simple-docblock.php');

        $descriptionText = <<<DESCRIPTION
This is a Description. A Summary and Description are separated by either
two subsequent newlines (thus a whiteline in between as can be seen in this
example), or when the Summary ends with a dot (`.`) and some form of
whitespace.
DESCRIPTION;

        $this->assertInstanceOf(DocBlock::class, $docblock);
        $this->assertSame('This is an example of a summary.', $summary);
        $this->assertInstanceOf(Description::class, $description);
        $this->assertSame(
            str_replace(
                PHP_EOL,
                "\n",
                $descriptionText
            ),
            $description->render()
        );
        $this->assertEmpty($docblock->getTags());
    }

    public function testInterpretingTags(): void
    {
        /** @var DocBlock $docblock */
        $docblock;
        /** @var boolean $hasSeeTag */
        $hasSeeTag;
        /** @var Tag[] $tags */
        $tags;
        /** @var See[] $seeTags */
        $seeTags;

        include(__DIR__ . '/../../examples/02-interpreting-tags.php');

        $this->assertTrue($hasSeeTag);
        $this->assertCount(1, $tags);
        $this->assertCount(1, $seeTags);

        $this->assertInstanceOf(See::class, $tags[0]);
        $this->assertInstanceOf(See::class, $seeTags[0]);

        $seeTag = $seeTags[0];
        $this->assertSame('\\' . StandardTagFactory::class, (string)$seeTag->getReference());
        $this->assertSame('', (string)$seeTag->getDescription());
    }

    public function testDescriptionsCanEscapeAtSignsAndClosingBraces(): void
    {
        /** @var string $docComment */
        $docComment;
        /** @var DocBlock $docblock */
        $docblock;
        /** @var Description $description */
        $description;
        /** @var string $receivedDocComment */
        $receivedDocComment;
        /** @var string $foundDescription */
        $foundDescription;

        include(__DIR__ . '/../../examples/playing-with-descriptions/02-escaping.php');

        $this->assertSame(
            str_replace(
                PHP_EOL,
                "\n",
                <<<'DESCRIPTION'
You can escape the @-sign by surrounding it with braces, for example: @. And escape a closing brace within an
inline tag by adding an opening brace in front of it like this: }.

Here are example texts where you can see how they could be used in a real life situation:

    This is a text with an {@internal inline tag where a closing brace (}) is shown}.
    Or an {@internal inline tag with a literal {@link} in it}.

Do note that an {@internal inline tag that has an opening brace ({) does not break out}.
DESCRIPTION
            ),
            $foundDescription
        );
    }

    public function testMultilineTags(): void
    {
        $docComment = <<<DOC
/**
 * This is an example of a summary.
 *
 * @param array<
 *   int,
 *   string     
 * > \$store
 */
DOC;
        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        self::assertEquals(
            [
                new Param(
                    'store',
                    new Array_(
                        new String_(),
                        new Integer()
                    ),
                    false,
                    new Description(''),
                ),
            ],
            $docblock->getTags()
        );

    }

    public function testMethodRegression(): void
    {
        $docComment = <<<DOC
/**
 * This is an example of a summary.
 *
 * @method void setInteger(integer \$integer)
 */
DOC;
        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        self::assertEquals(
            [
                new Method(
                    'setInteger',
                    [],
                    new Void_(),
                    false,
                    new Description(''),
                    false,
                    [
                        new MethodParameter('integer', new Integer())
                    ]
                ),
            ],
            $docblock->getTags()
        );
    }

    public function testInvalidTypeParamResultsInInvalidTag(): void
    {
        $docComment = '
/**
 * This is an example of a summary.
 *
 * @param array\Foo> $test
 */
';
        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        self::assertEquals(
            [
                InvalidTag::create(
                    'array\Foo> $test',
                    'param',
                )->withError(
                    new \InvalidArgumentException(
                        'Could not find type in array\Foo> $test, please check for malformed notations')
                ),
            ],
            $docblock->getTags()
        );
    }

    public function testConstantReferenceTypes(): void
    {
        $docComment = <<<DOC
    /**
     * @return self::STATUS_*
     */
DOC;

        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        self::assertEquals(
            [
                new Return_(new ConstExpression(new Self_(), 'STATUS_*'), new Description('')),
            ],
            $docblock->getTags()
        );
    }

    #[Group('noparser2')]
    public function testRegressionWordpressDocblocks(): void
    {
        $docComment = <<<DOC
    /**
     * Install a package.
     *
     * Copies the contents of a package from a source directory, and installs them in
     * a destination directory. Optionally removes the source. It can also optionally
     * clear out the destination folder if it already exists.
     *
     * @since 2.8.0
     * @since 6.2.0 Use move_dir() instead of copy_dir() when possible.
     *
     * @global WP_Filesystem_Base \$wp_filesystem        WordPress filesystem subclass.
     * @global array              \$wp_theme_directories
     *
     * @param array|string \$args {
     *     Optional. Array or string of arguments for installing a package. Default empty array.
     *
     *     @type string \$source                      Required path to the package source. Default empty.
     *     @type string \$destination                 Required path to a folder to install the package in.
     *                                               Default empty.
     *     @type bool   \$clear_destination           Whether to delete any files already in the destination
     *                                               folder. Default false.
     *     @type bool   \$clear_working               Whether to delete the files from the working directory
     *                                               after copying them to the destination. Default false.
     *     @type bool   \$abort_if_destination_exists Whether to abort the installation if
     *                                               the destination folder already exists. Default true.
     *     @type array  \$hook_extra                  Extra arguments to pass to the filter hooks called by
     *                                               WP_Upgrader::install_package(). Default empty array.
     * }
     *
     * @return array|WP_Error The result (also stored in `WP_Upgrader::\$result`), or a WP_Error on failure.
     */
DOC;

        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        self::assertEquals(
            new DocBlock(
                'Install a package.',
                new Description(
                    'Copies the contents of a package from a source directory, and installs them in' . PHP_EOL .
                    'a destination directory. Optionally removes the source. It can also optionally' . PHP_EOL .
                    'clear out the destination folder if it already exists.'
                ),
                [
                    new Since('2.8.0', new Description('')),
                    new Since('6.2.0', new Description('Use move_dir() instead of copy_dir() when possible.')),
                    new Generic(
                        'global',
                        new Description('WP_Filesystem_Base $wp_filesystem        WordPress filesystem subclass.')
                    ),
                    new Generic(
                        'global',
                        new Description('array              $wp_theme_directories')
                    ),
                    new Param(
                        'args',
                        new Compound([new Array_(new Mixed_()), new String_()]),
                        false,
                        new Description(
                            '{' . "\n" .
                            '    Optional. Array or string of arguments for installing a package. Default empty array.' . "\n" .
                            "\n" .
                            '    @type string $source                      Required path to the package source. Default empty.' . "\n" .
                            '    @type string $destination                 Required path to a folder to install the package in.' . "\n" .
                            '                                              Default empty.' . "\n" .
                            '    @type bool   $clear_destination           Whether to delete any files already in the destination' . "\n" .
                            '                                              folder. Default false.' . "\n" .
                            '    @type bool   $clear_working               Whether to delete the files from the working directory' . "\n" .
                            '                                              after copying them to the destination. Default false.' . "\n" .
                            '    @type bool   $abort_if_destination_exists Whether to abort the installation if' . "\n" .
                            '                                              the destination folder already exists. Default true.' . "\n" .
                            '    @type array  $hook_extra                  Extra arguments to pass to the filter hooks called by' . "\n" .
                            '                                              WP_Upgrader::install_package(). Default empty array.' . "\n" .
                            '}'
                        )
                    ),
                    new Return_(
                        new Compound([new Array_(new Mixed_()), new Object_(new Fqsen('\WP_Error'))]),
                        new Description('The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.')
                    ),
                ],
                new Context('\\')
            ),
            $docblock
        );
    }

    #[Group('noparser2')]
    public function testIndentationIsKept(): void
    {
        $docComment = <<<DOC
	/**
	 * Registers the script module if no script module with that script module
	 * identifier has already been registered.
	 *
	 * @since 6.5.0
	 *
	 * @param array             \$deps     {
	 *                                        Optional. List of dependencies.
	 *
	 *                                        @type string|array ...$0 {
	 *                                            An array of script module identifiers of the dependencies of this script
	 *                                            module. The dependencies can be strings or arrays. If they are arrays,
	 *                                            they need an `id` key with the script module identifier, and can contain
	 *                                            an `import` key with either `static` or `dynamic`. By default,
	 *                                            dependencies that don't contain an `import` key are considered static.
	 *
	 *                                            @type string \$id     The script module identifier.
	 *                                            @type string \$import Optional. Import type. May be either `static` or
	 *                                                                 `dynamic`. Defaults to `static`.
	 *                                        }
	 *                                    }
	 */
DOC;

        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        self::assertEquals(
            new DocBlock(
                'Registers the script module if no script module with that script module
identifier has already been registered.',
                new Description(
                    ''
                ),
                [
                    new Since('6.5.0', new Description('')),
                    new Param(
                        'deps',
                        new Array_(new Mixed_()),
                        false,
                        new Description("{
    Optional. List of dependencies.

    @type string|array ...$0 {
        An array of script module identifiers of the dependencies of this script
        module. The dependencies can be strings or arrays. If they are arrays,
        they need an `id` key with the script module identifier, and can contain
        an `import` key with either `static` or `dynamic`. By default,
        dependencies that don't contain an `import` key are considered static.

        @type string \$id     The script module identifier.
        @type string \$import Optional. Import type. May be either `static` or
                             `dynamic`. Defaults to `static`.
    }
}"
                        )
                    ),
                ],
                new Context('\\')
            ),
            $docblock
        );
    }

    public function testProcessTemplateTag(): void
    {
        $docComment = <<<DOCBLOCK
        /** 
         * @template T as \Type this is a description
         * @template TDefault as \Type = \\String_ this is a description
         */
DOCBLOCK;

        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        self::assertEquals(
            [
                new Template(
                    'T',
                    new Object_(new Fqsen('\\Type')),
                    new Mixed_(),
                    new Description('this is a description')
                ),
                new Template(
                    'TDefault',
                    new Object_(new Fqsen('\\Type')),
                    new Object_(new Fqsen('\\String_')),
                    new Description('this is a description')
                ),
            ],
            $docblock->getTags()
        );
    }

    public function testParamTagDescriptionIsCorrectly(): void
    {
        $docComment = '
	/**
	 * @param int $baz Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius, tellus in cursus
	 *     dictum, justo odio sagittis velit, id iaculis mi dui id nisi.
	 */
';

        $factory = DocBlockFactory::createInstance();
        $docblock = $factory->create($docComment);

        $paramTags = $docblock->getTagsWithTypeByName('param')[0];

        self::assertSame('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas varius, tellus in cursus
dictum, justo odio sagittis velit, id iaculis mi dui id nisi.', (string) $paramTags->getDescription());
    }
}