Completed
Pull Request — master (#120)
by Jaap
01:49
created

ExampleFinder::setExampleDirectories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Reflection\DocBlock;
14
15
use phpDocumentor\Reflection\DocBlock\Tags\Example;
16
17
/**
18
 * Class used to find an example file's location based on a given ExampleDescriptor.
19
 */
20
class ExampleFinder
21
{
22
    /** @var string */
23
    private $sourceDirectory = '';
24
25
    /** @var string[] */
26
    private $exampleDirectories = array();
27
28
    /**
29
     * Attempts to find the example contents for the given descriptor.
30
     *
31
     * @param Example $example
32
     *
33
     * @return string
34
     */
35 1
    public function find(Example $example)
36
    {
37 1
        $filename = $example->getFilePath();
38
39 1
        $file = $this->getExampleFileContents($filename);
40 1
        if (!$file) {
41 1
            return "** File not found : {$filename} **";
42
        }
43
44
        return implode('', array_slice($file, $example->getStartingLine() - 1, $example->getLineCount()));
45
    }
46
47
    /**
48
     * Registers the project's root directory where an 'examples' folder can be expected.
49
     *
50
     * @param string $directory
51
     *
52
     * @return void
53
     */
54
    public function setSourceDirectory($directory = '')
55
    {
56
        $this->sourceDirectory = $directory;
57
    }
58
59
    /**
60
     * Returns the project's root directory where an 'examples' folder can be expected.
61
     *
62
     * @return string
63
     */
64 1
    public function getSourceDirectory()
65
    {
66 1
        return $this->sourceDirectory;
67
    }
68
69
    /**
70
     * Registers a series of directories that may contain examples.
71
     *
72
     * @param string[] $directories
73
     */
74
    public function setExampleDirectories(array $directories)
75
    {
76
        $this->exampleDirectories = $directories;
77
    }
78
79
    /**
80
     * Returns a series of directories that may contain examples.
81
     *
82
     * @return string[]
83
     */
84
    public function getExampleDirectories()
85
    {
86
        return $this->exampleDirectories;
87
    }
88
89
    /**
90
     * Attempts to find the requested example file and returns its contents or null if no file was found.
91
     *
92
     * This method will try several methods in search of the given example file, the first one it encounters is
93
     * returned:
94
     *
95
     * 1. Iterates through all examples folders for the given filename
96
     * 2. Checks the source folder for the given filename
97
     * 3. Checks the 'examples' folder in the current working directory for examples
98
     * 4. Checks the path relative to the current working directory for the given filename
99
     *
100
     * @param string $filename
101
     *
102
     * @return string|null
103
     */
104 1
    private function getExampleFileContents($filename)
105
    {
106 1
        $normalizedPath = null;
107
108 1
        foreach ($this->exampleDirectories as $directory) {
109
            $exampleFileFromConfig = $this->constructExamplePath($directory, $filename);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $exampleFileFromConfig exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
110
            if (is_readable($exampleFileFromConfig)) {
111
                $normalizedPath = $exampleFileFromConfig;
112
                break;
113
            }
114
        }
115
116 1
        if (!$normalizedPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $normalizedPath of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
117 1
            if (is_readable($this->getExamplePathFromSource($filename))) {
118
                $normalizedPath = $this->getExamplePathFromSource($filename);
119 1
            } elseif (is_readable($this->getExamplePathFromExampleDirectory($filename))) {
120
                $normalizedPath = $this->getExamplePathFromExampleDirectory($filename);
121 1
            } elseif (is_readable($filename)) {
122
                $normalizedPath = $filename;
123
            }
124
        }
125
126 1
        return $normalizedPath && is_readable($normalizedPath) ? file($normalizedPath) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression $normalizedPath of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
127
    }
128
129
    /**
130
     * Get example filepath based on the example directory inside your project.
131
     *
132
     * @param string $file
133
     *
134
     * @return string
135
     */
136 1
    private function getExamplePathFromExampleDirectory($file)
137
    {
138 1
        return getcwd() . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $file;
139
    }
140
141
    /**
142
     * Returns a path to the example file in the given directory..
143
     *
144
     * @param string $directory
145
     * @param string $file
146
     *
147
     * @return string
148
     */
149
    private function constructExamplePath($directory, $file)
150
    {
151
        return rtrim($directory, '\\/') . DIRECTORY_SEPARATOR . $file;
152
    }
153
154
    /**
155
     * Get example filepath based on sourcecode.
156
     *
157
     * @param string $file
158
     *
159
     * @return string
160
     */
161 1
    private function getExamplePathFromSource($file)
162
    {
163 1
        return sprintf(
164 1
            '%s%s%s',
165 1
            trim($this->getSourceDirectory(), '\\/'),
166 1
            DIRECTORY_SEPARATOR,
167 1
            trim($file, '"')
168
        );
169
    }
170
}
171