Skip to content

Commit a4111f0

Browse files
Merge pull request #35 from Oussama-Tn/patch-1
Remove useless if condition
2 parents ef512cc + 6d5a1d9 commit a4111f0

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

src/Console/GenerateCommand.php

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -252,36 +252,35 @@ protected function getPropertiesFromTable($model)
252252
protected function getPropertiesFromMethods($model)
253253
{
254254
$methods = get_class_methods($model);
255-
if ($methods) {
256-
foreach ($methods as $method) {
257-
if (!method_exists('Illuminate\Database\Eloquent\Model', $method) && !Str::startsWith($method, 'get')) {
258-
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
259-
$reflection = new \ReflectionMethod($model, $method);
260-
$file = new \SplFileObject($reflection->getFileName());
261-
$file->seek($reflection->getStartLine() - 1);
262-
$code = '';
263-
while ($file->key() < $reflection->getEndLine()) {
264-
$code .= $file->current();
265-
$file->next();
266-
}
267-
$code = trim(preg_replace('/\s\s+/', '', $code));
268-
$begin = strpos($code, 'function(');
269-
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
270-
foreach (array(
271-
'belongsTo',
272-
) as $relation) {
273-
$search = '$this->' . $relation . '(';
274-
if ($pos = stripos($code, $search)) {
275-
//Resolve the relation's model to a Relation object.
276-
$relationObj = $model->$method();
277-
if ($relationObj instanceof Relation) {
278-
$relatedModel = '\\' . get_class($relationObj->getRelated());
279-
$relatedObj = new $relatedModel;
280-
$property = property_exists($relationObj, 'getForeignKeyName')
281-
? $relationObj->getForeignKeyName()
282-
: $relationObj->getForeignKey();
283-
$this->setProperty($property, 'factory(' . get_class($relationObj->getRelated()) . '::class)->create()->' . $relatedObj->getKeyName());
284-
}
255+
256+
foreach ($methods as $method) {
257+
if (!method_exists('Illuminate\Database\Eloquent\Model', $method) && !Str::startsWith($method, 'get')) {
258+
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
259+
$reflection = new \ReflectionMethod($model, $method);
260+
$file = new \SplFileObject($reflection->getFileName());
261+
$file->seek($reflection->getStartLine() - 1);
262+
$code = '';
263+
while ($file->key() < $reflection->getEndLine()) {
264+
$code .= $file->current();
265+
$file->next();
266+
}
267+
$code = trim(preg_replace('/\s\s+/', '', $code));
268+
$begin = strpos($code, 'function(');
269+
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
270+
foreach (array(
271+
'belongsTo',
272+
) as $relation) {
273+
$search = '$this->' . $relation . '(';
274+
if ($pos = stripos($code, $search)) {
275+
//Resolve the relation's model to a Relation object.
276+
$relationObj = $model->$method();
277+
if ($relationObj instanceof Relation) {
278+
$relatedModel = '\\' . get_class($relationObj->getRelated());
279+
$relatedObj = new $relatedModel;
280+
$property = property_exists($relationObj, 'getForeignKeyName')
281+
? $relationObj->getForeignKeyName()
282+
: $relationObj->getForeignKey();
283+
$this->setProperty($property, 'factory(' . get_class($relationObj->getRelated()) . '::class)->create()->' . $relatedObj->getKeyName());
285284
}
286285
}
287286
}

0 commit comments

Comments
 (0)