Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.

Refactor serveral duplicate loops into a method #52

Merged
merged 1 commit into from
Jan 3, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,59 +34,31 @@ public function __construct()

public function isMethodStatic(File $file, LineRange $range)
{
$this->broker = new Broker(new Memory);
$file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true);
$lastLine = $range->getEnd();

foreach ($file->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $class) {
foreach ($class->getMethods() as $method) {
if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) {
return $method->isStatic();
}
}
}
}
$method = $this->findMatchingMethod($file, $range);

return false;
return $method ? $method->isStatic() : false;
}

public function getMethodEndLine(File $file, LineRange $range)
{
$this->broker = new Broker(new Memory);
$file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true);
$lastLine = $range->getEnd();
$method = $this->findMatchingMethod($file, $range);

foreach ($file->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $class) {
foreach ($class->getMethods() as $method) {
if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) {
return $method->getEndLine();
}
}
}
if ($method === null) {
throw new \InvalidArgumentException("Could not find method end line.");
}

throw new \InvalidArgumentException("Could not find method end line.");
return $method->getEndLine();
}

public function getMethodStartLine(File $file, LineRange $range)
{
$this->broker = new Broker(new Memory);
$file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true);
$lastLine = $range->getEnd();
$method = $this->findMatchingMethod($file, $range);

foreach ($file->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $class) {
foreach ($class->getMethods() as $method) {
if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) {
return $method->getStartLine();
}
}
}
if ($method === null) {
throw new \InvalidArgumentException("Could not find method start line.");
}

throw new \InvalidArgumentException("Could not find method start line.");
return $method->getStartLine();
}

public function getLineOfLastPropertyDefinedInScope(File $file, $lastLine)
Expand Down Expand Up @@ -115,20 +87,7 @@ public function getLineOfLastPropertyDefinedInScope(File $file, $lastLine)

public function isInsideMethod(File $file, LineRange $range)
{
$this->broker = new Broker(new Memory);
$file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true);

foreach ($file->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $class) {
foreach ($class->getMethods() as $method) {
if ($method->getStartLine() < $range->getStart() && $range->getEnd() < $method->getEndLine()) {
return true;
}
}
}
}

return false;
return $this->findMatchingMethod($file, $range) !== null;
}

/**
Expand All @@ -154,4 +113,26 @@ public function findClasses(File $file)

return $classes;
}

private function findMatchingMethod(File $file, LineRange $range)
{
$foundMethod = null;

$this->broker = new Broker(new Memory);
$file = $this->broker->processString($file->getCode(), $file->getRelativePath(), true);
$lastLine = $range->getEnd();

foreach ($file->getNamespaces() as $namespace) {
foreach ($namespace->getClasses() as $class) {
foreach ($class->getMethods() as $method) {
if ($method->getStartLine() < $lastLine && $lastLine < $method->getEndLine()) {
$foundMethod = $method;
break;
}
}
}
}

return $foundMethod;
}
}