Skip to content

Commit

Permalink
Extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
guidocella committed Feb 18, 2017
1 parent eda88be commit 73a06b5
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/ModelPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,7 @@ protected function getRelations()
continue;
}

$reflection = new \ReflectionMethod($this->model, $method);

$file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1);

$methodCode = '';
while ($file->key() < $reflection->getEndLine()) {
$methodCode .= $file->current();
$file->next();
}
$methodCode = trim(preg_replace('/\s\s+/', '', $methodCode));
$begin = strpos($methodCode, 'function(');
$length = strrpos($methodCode, '}') - $begin + 1;
$methodCode = substr($methodCode, $begin, $length);
$methodCode = $this->getMethodCode($method);

foreach ([
'belongsTo',
Expand All @@ -217,6 +204,35 @@ protected function getRelations()
return $relations;
}

/**
* Get the source code of a method of the model.
*
* @param string $method
* @return string
*/
protected function getMethodCode($method)
{
$reflection = new \ReflectionMethod($this->model, $method);

$file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1);

$methodCode = '';

while ($file->key() < $reflection->getEndLine()) {
$methodCode .= $file->current();

$file->next();
}

$methodCode = trim(preg_replace('/\s\s+/', '', $methodCode));

$begin = strpos($methodCode, 'function(');
$length = strrpos($methodCode, '}') - $begin + 1;

return substr($methodCode, $begin, $length);
}

/**
* Get the model's Belongs To relations.
*
Expand Down

0 comments on commit 73a06b5

Please sign in to comment.