Passed
Pull Request — master (#6869)
by Michael
09:57
created

setDefaultRepositoryName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
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
 * @deprecated
36
 */
37
class EntityRepositoryGenerator
38
{
39
    private $repositoryName;
40
41
    protected static $_template =
42
'<?php
43
44
<namespace>
45
46
/**
47
 * <className>
48
 *
49
 * This class was generated by the Doctrine ORM. Add your own custom
50
 * repository methods below.
51
 */
52
class <className> extends <repositoryName>
53
{
54
}
55
';
56
57 4
    public function __construct()
58
    {
59 4
        @trigger_error(self::class . ' is deprecated and will be removed in Doctrine 3.0', E_USER_DEPRECATED);
60 4
    }
61
62
    /**
63
     * @param string $fullClassName
64
     *
65
     * @return string
66
     */
67 4
    public function generateEntityRepositoryClass($fullClassName)
68
    {
69
        $variables = [
70 4
            '<namespace>'       => $this->generateEntityRepositoryNamespace($fullClassName),
71 4
            '<repositoryName>'  => $this->generateEntityRepositoryName($fullClassName),
72 4
            '<className>'       => $this->generateClassName($fullClassName)
73
        ];
74
75 4
        return str_replace(array_keys($variables), array_values($variables), self::$_template);
76
    }
77
78
    /**
79
     * Generates the namespace, if class do not have namespace, return empty string instead.
80
     *
81
     * @param string $fullClassName
82
     *
83
     * @return string $namespace
84
     */
85 4
    private function getClassNamespace($fullClassName)
86
    {
87 4
        $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

87
        $namespace = substr($fullClassName, 0, /** @scrutinizer ignore-type */ strrpos($fullClassName, '\\'));
Loading history...
88
89 4
        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...
90
    }
91
92
    /**
93
     * Generates the class name
94
     *
95
     * @param string $fullClassName
96
     *
97
     * @return string
98
     */
99 4
    private function generateClassName($fullClassName)
100
    {
101 4
        $namespace = $this->getClassNamespace($fullClassName);
102
103 4
        $className = $fullClassName;
104
105 4
        if ($namespace) {
106 4
            $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
107
        }
108
109 4
        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...
110
    }
111
112
    /**
113
     * Generates the namespace statement, if class do not have namespace, return empty string instead.
114
     *
115
     * @param string $fullClassName The full repository class name.
116
     *
117
     * @return string $namespace
118
     */
119 4
    private function generateEntityRepositoryNamespace($fullClassName)
120
    {
121 4
        $namespace = $this->getClassNamespace($fullClassName);
122
123 4
        return $namespace ? 'namespace ' . $namespace . ';' : '';
124
    }
125
126
    /**
127
     * @param string $fullClassName
128
     *
129
     * @return string $repositoryName
130
     */
131 4
    private function generateEntityRepositoryName($fullClassName)
132
    {
133 4
        $namespace = $this->getClassNamespace($fullClassName);
134
135 4
        $repositoryName = $this->repositoryName ?: EntityRepository::class;
136
137 4
        if ($namespace && $repositoryName[0] !== '\\') {
138 4
            $repositoryName = '\\' . $repositoryName;
139
        }
140
141 4
        return $repositoryName;
142
    }
143
144
    /**
145
     * @param string $fullClassName
146
     * @param string $outputDirectory
147
     *
148
     * @return void
149
     */
150 4
    public function writeEntityRepositoryClass($fullClassName, $outputDirectory)
151
    {
152 4
        $code = $this->generateEntityRepositoryClass($fullClassName);
153
154 4
        $path = $outputDirectory . DIRECTORY_SEPARATOR
155 4
              . str_replace('\\', \DIRECTORY_SEPARATOR, $fullClassName) . '.php';
156 4
        $dir = dirname($path);
157
158 4
        if ( ! is_dir($dir)) {
159 2
            mkdir($dir, 0775, true);
160
        }
161
162 4
        if ( ! file_exists($path)) {
163 4
            file_put_contents($path, $code);
164 4
            chmod($path, 0664);
165
        }
166 4
    }
167
168
    /**
169
     * @param string $repositoryName
170
     *
171
     * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator
172
     */
173 4
    public function setDefaultRepositoryName($repositoryName)
174
    {
175 4
        $this->repositoryName = $repositoryName;
176
177 4
        return $this;
178
    }
179
180
}
181