Completed
Pull Request — master (#570)
by Marcel
01:55
created

DocBlockParamHelpers   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldExcludeExample() 0 4 1
A parseParamDescription() 0 12 2
1
<?php
2
3
namespace Mpociot\ApiDoc\Tools\Traits;
4
5
use Mpociot\Reflection\DocBlock\Tag;
6
7
trait DocBlockParamHelpers
8
{
9
    use ParamHelpers;
10
11
    /**
12
     * Allows users to specify that we shouldn't generate an example for the parameter
13
     * by writing 'No-example'.
14
     *
15
     * @param Tag $tag
16
     *
17
     * @return bool Whether no example should be generated
18
     */
19
    protected function shouldExcludeExample(Tag $tag)
20
    {
21
        return strpos($tag->getContent(), ' No-example') !== false;
22
    }
23
24
    /**
25
     * Allows users to specify an example for the parameter by writing 'Example: the-example',
26
     * to be used in example requests and response calls.
27
     *
28
     * @param string $description
29
     * @param string $type The type of the parameter. Used to cast the example provided, if any.
30
     *
31
     * @return array The description and included example.
32
     */
33
    protected function parseParamDescription(string $description, string $type)
34
    {
35
        $example = null;
36
        if (preg_match('/(.*)\s+Example:\s*(.*)\s*/', $description, $content)) {
37
            $description = $content[1];
38
39
            // examples are parsed as strings by default, we need to cast them properly
40
            $example = $this->castToType($content[2], $type);
41
        }
42
43
        return [$description, $example];
44
    }
45
}
46