This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
/
ReplaceWithMethodCall.php
79 lines (64 loc) · 1.87 KB
/
ReplaceWithMethodCall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace QafooLabs\Refactoring\Domain\Model\EditingAction;
use QafooLabs\Refactoring\Domain\Model\EditingAction;
use QafooLabs\Refactoring\Domain\Model\EditorBuffer;
use QafooLabs\Refactoring\Domain\Model\MethodSignature;
use QafooLabs\Refactoring\Domain\Model\LineRange;
class ReplaceWithMethodCall implements EditingAction
{
/**
* @var LineRange
*/
private $range;
/**
* @var MethodSignature
*/
private $newMethod;
public function __construct(LineRange $range, MethodSignature $newMethod)
{
$this->range = $range;
$this->newMethod = $newMethod;
}
public function performEdit(EditorBuffer $buffer)
{
$buffer->replace($this->range, array($this->getIndent() . $this->getMethodCall()));
}
private function getIndent()
{
return ' ';
}
private function getMethodCall()
{
return sprintf(
'%s%s%s(%s);',
$this->getReturnVariables(),
($this->newMethod->isStatic() ? 'self::' : '$this->'),
$this->newMethod->getName(),
$this->createVariableList($this->newMethod->arguments())
);
}
private function getReturnVariables()
{
$returnVars = $this->newMethod->returnVariables();
$numVariables = count($returnVars);
if ($numVariables === 0) {
return;
}
$returnVariable = '$' . reset($returnVars);
if ($numVariables > 1) {
$returnVariable = 'list(' . $this->createVariableList($returnVars) . ')';
}
return $returnVariable . ' = ';
}
/**
* @param string[] $variables
*
* @return string
*/
private function createVariableList(array $variables)
{
return implode(', ', array_map(function ($variableName) {
return '$' . $variableName;
}, $variables));
}
}