Completed
Pull Request — master (#6869)
by Michael
228:23 queued 163:26
created

generateEntityRepositoryName()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 3
cts 3
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools;
21
22
use Doctrine\ORM\EntityRepository;
23
24
/**
25
 * Class to generate entity repository classes
26
 *
27
 *
28
 * @link    www.doctrine-project.org
29
 * @since   2.0
30
 * @author  Benjamin Eberlei <[email protected]>
31
 * @author  Guilherme Blanco <[email protected]>
32
 * @author  Jonathan Wage <[email protected]>
33
 * @author  Roman Borschel <[email protected]>
34
 */
35
class EntityRepositoryGenerator
36
{
37
    private $repositoryName;
38
39
    protected static $_template =
40
'<?php
41
42
<namespace>
43
44
/**
45
 * <className>
46
 *
47
 * This class was generated by the Doctrine ORM. Add your own custom
48
 * repository methods below.
49
 */
50
class <className> extends <repositoryName>
51
{
52
}
53
';
54
55
    public function __construct()
56
    {
57
        @trigger_error(self::class . ' is deprecated and will be removed in Doctrine 3.0', E_USER_DEPRECATED);
58
    }
59
60 4
    /**
61
     * @param string $fullClassName
62
     *
63 4
     * @return string
64 4
     */
65 4
    public function generateEntityRepositoryClass($fullClassName)
66
    {
67
        $variables = [
68 4
            '<namespace>'       => $this->generateEntityRepositoryNamespace($fullClassName),
69
            '<repositoryName>'  => $this->generateEntityRepositoryName($fullClassName),
70
            '<className>'       => $this->generateClassName($fullClassName)
71
        ];
72
73
        return str_replace(array_keys($variables), array_values($variables), self::$_template);
74
    }
75
76
    /**
77
     * Generates the namespace, if class do not have namespace, return empty string instead.
78 4
     *
79
     * @param string $fullClassName
80 4
     *
81
     * @return string $namespace
82 4
     */
83
    private function getClassNamespace($fullClassName)
84
    {
85
        $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
0 ignored issues
show
Bug introduced by
It seems like strrpos($fullClassName, '\') can also be of type false; however, parameter $length of substr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $namespace = substr($fullClassName, 0, /** @scrutinizer ignore-type */ strrpos($fullClassName, '\\'));
Loading history...
86
87
        return $namespace;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $namespace could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
88
    }
89
90
    /**
91
     * Generates the class name
92 4
     *
93
     * @param string $fullClassName
94 4
     *
95
     * @return string
96 4
     */
97
    private function generateClassName($fullClassName)
98 4
    {
99 4
        $namespace = $this->getClassNamespace($fullClassName);
100
101
        $className = $fullClassName;
102 4
103
        if ($namespace) {
104
            $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
105
        }
106
107
        return $className;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $className could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
108
    }
109
110
    /**
111
     * Generates the namespace statement, if class do not have namespace, return empty string instead.
112 4
     *
113
     * @param string $fullClassName The full repository class name.
114 4
     *
115
     * @return string $namespace
116 4
     */
117
    private function generateEntityRepositoryNamespace($fullClassName)
118
    {
119
        $namespace = $this->getClassNamespace($fullClassName);
120
121
        return $namespace ? 'namespace ' . $namespace . ';' : '';
122
    }
123
124 4
    /**
125
     * @param string $fullClassName
126 4
     *
127
     * @return string $repositoryName
128 4
     */
129
    private function generateEntityRepositoryName($fullClassName)
130 4
    {
131 4
        $namespace = $this->getClassNamespace($fullClassName);
132
133
        $repositoryName = $this->repositoryName ?: EntityRepository::class;
134 4
135
        if ($namespace && $repositoryName[0] !== '\\') {
136
            $repositoryName = '\\' . $repositoryName;
137
        }
138
139
        return $repositoryName;
140
    }
141
142
    /**
143 4
     * @param string $fullClassName
144
     * @param string $outputDirectory
145 4
     *
146
     * @return void
147 4
     */
148 4
    public function writeEntityRepositoryClass($fullClassName, $outputDirectory)
149 4
    {
150
        $code = $this->generateEntityRepositoryClass($fullClassName);
151 4
152 2
        $path = $outputDirectory . DIRECTORY_SEPARATOR
153
              . str_replace('\\', \DIRECTORY_SEPARATOR, $fullClassName) . '.php';
154
        $dir = dirname($path);
155 4
156 4
        if ( ! is_dir($dir)) {
157 4
            mkdir($dir, 0775, true);
158
        }
159 4
160
        if ( ! file_exists($path)) {
161
            file_put_contents($path, $code);
162
            chmod($path, 0664);
163
        }
164
    }
165
166 4
    /**
167
     * @param string $repositoryName
168 4
     *
169
     * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator
170 4
     */
171
    public function setDefaultRepositoryName($repositoryName)
172
    {
173
        $this->repositoryName = $repositoryName;
174
175
        return $this;
176
    }
177
178
}
179