Completed
Pull Request — master (#570)
by
unknown
01:24
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 Faker\Factory;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Mpociot\Reflection\DocBlock\Tag;
9
10
trait DocBlockParamHelpers
11
{
12
    use ParamHelpers;
13
14
    /**
15
     * Allows users to specify that we shouldn't generate an example for the parameter
16
     * by writing 'No-example'.
17
     *
18
     * @param Tag $tag
19
     *
20
     * @return bool Whether no example should be generated
21
     */
22
    protected  function shouldExcludeExample(Tag $tag)
23
    {
24
        return strpos($tag->getContent(), ' No-example') !== false;
25
    }
26
27
    /**
28
     * Allows users to specify an example for the parameter by writing 'Example: the-example',
29
     * to be used in example requests and response calls.
30
     *
31
     * @param string $description
32
     * @param string $type The type of the parameter. Used to cast the example provided, if any.
33
     *
34
     * @return array The description and included example.
35
     */
36
    protected  function parseParamDescription(string $description, string $type)
37
    {
38
        $example = null;
39
        if (preg_match('/(.*)\s+Example:\s*(.*)\s*/', $description, $content)) {
40
            $description = $content[1];
41
42
            // examples are parsed as strings by default, we need to cast them properly
43
            $example = $this->castToType($content[2], $type);
44
        }
45
46
        return [$description, $example];
47
    }
48
}
49