public function enterNode(PHPParser_Node $node) { if (!$this->lineRange->isInRange($node->getLine())) { return; } $parent = $node->getAttribute('parent'); // TODO: Expensive (?) do { if ($parent && $this->statements->contains($parent)) { return; } } while ($parent && ($parent = $parent->getAttribute('parent'))); $this->statements->attach($node); }
public function testExtractRangeIndentsMethodCallForFirstLineWithExtraIndent() { $lines = array(' echo "Something";'); $this->buffer->expects($this->once())->method('getLines')->will($this->returnValue($lines)); $action = new ReplaceWithMethodCall(LineRange::fromLines(1, 2), new MethodSignature('testMethod')); $this->assertGeneratedMethodCallMatches('$this->testMethod();', $action, 12); }
protected function execute(InputInterface $input, OutputInterface $output) { $file = File::createFromPath($input->getArgument('file'), getcwd()); $range = LineRange::fromString($input->getArgument('range')); $newMethodName = $input->getArgument('newmethod'); $scanner = new ParserVariableScanner(); $codeAnalysis = new StaticCodeAnalysis(); $editor = new PatchEditor(new OutputPatchCommand($output)); $extractMethod = new ExtractMethod($scanner, $codeAnalysis, $editor); $extractMethod->refactor($file, $range, $newMethodName); }
/** * @group regression * @group GH-4 */ public function testVariableUsedBeforeAndAfterExtractedSlice() { $this->markTestIncomplete('Failing over some invisible whitespace issue?'); $patch = $this->refactoring->refactor(new File("foo.php", <<<'PHP' <?php class Foo { public function main() { $foo = "bar"; $baz = array(); $foo = strtolower($foo); $baz[] = $foo; return new Something($foo, $baz); } } PHP ), LineRange::fromString("9-10"), "extract"); \Phake::verify($this->applyCommand)->apply(<<<'CODE' --- a/foo.php +++ b/foo.php @@ -6,9 +6,16 @@ $foo = "bar"; $baz = array(); + list($foo, $baz) = $this->extract($foo, $baz); + + return new Something($foo, $baz); + } + + private function extract($foo, $baz) + { $foo = strtolower($foo); $baz[] = $foo; - return new Something($foo, $baz); + return array($foo, $baz); } } CODE ); }
public function testRenameLocalVariable() { $buffer = \Phake::mock('QafooLabs\\Refactoring\\Domain\\Model\\EditorBuffer'); \Phake::when($this->scanner)->scanForVariables(\Phake::anyParameters())->thenReturn(new DefinedVariables(array('helloWorld' => array(6)))); \Phake::when($this->editor)->openBuffer(\Phake::anyParameters())->thenReturn($buffer); \Phake::when($this->codeAnalysis)->findMethodRange(\Phake::anyParameters())->thenReturn(LineRange::fromSingleLine(1)); $patch = $this->refactoring->refactor(new File("foo.php", <<<'PHP' <?php class Foo { public function main() { $helloWorld = 'bar'; } } PHP ), 6, new Variable('$helloWorld'), new Variable('$var')); \Phake::verify($buffer)->replaceString(6, '$helloWorld', '$var'); }
/** * From a range within a method, find the start and end range of that method. * * @param File $file * @param LineRange $range * * @return LineRange */ public function findMethodRange(File $file, LineRange $range) { $methodStartLine = $this->getMethodStartLine($file, $range); $methodEndLine = $this->getMethodEndLine($file, $range); return LineRange::fromLines($methodStartLine, $methodEndLine); }
public function replace(LineRange $range, array $newLines) { $this->builder->replaceLines($range->getStart(), $range->getEnd(), $newLines); }
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; }
protected function getDefinedVariables() { $selectedMethodLineRange = $this->codeAnalysis->findMethodRange($this->file, LineRange::fromSingleLine($this->line)); $definedVariables = $this->variableScanner->scanForVariables($this->file, $selectedMethodLineRange); return $definedVariables; }
public static function rangeIsNotInsideMethod(LineRange $range) { return new self(sprintf('The range %d-%d is not inside one single method.', $range->getStart(), $range->getEnd())); }
private function getSelectedCode() { return LineCollection::createFromArray($this->extractRange->sliceCode($this->file->getCode())); }