@@ -34,59 +34,31 @@ public function __construct()
34
34
35
35
public function isMethodStatic (File $ file , LineRange $ range )
36
36
{
37
- $ this ->broker = new Broker (new Memory );
38
- $ file = $ this ->broker ->processString ($ file ->getCode (), $ file ->getRelativePath (), true );
39
- $ lastLine = $ range ->getEnd ();
40
-
41
- foreach ($ file ->getNamespaces () as $ namespace ) {
42
- foreach ($ namespace ->getClasses () as $ class ) {
43
- foreach ($ class ->getMethods () as $ method ) {
44
- if ($ method ->getStartLine () < $ lastLine && $ lastLine < $ method ->getEndLine ()) {
45
- return $ method ->isStatic ();
46
- }
47
- }
48
- }
49
- }
37
+ $ method = $ this ->findMatchingMethod ($ file , $ range );
50
38
51
- return false ;
39
+ return $ method ? $ method -> isStatic () : false ;
52
40
}
53
41
54
42
public function getMethodEndLine (File $ file , LineRange $ range )
55
43
{
56
- $ this ->broker = new Broker (new Memory );
57
- $ file = $ this ->broker ->processString ($ file ->getCode (), $ file ->getRelativePath (), true );
58
- $ lastLine = $ range ->getEnd ();
44
+ $ method = $ this ->findMatchingMethod ($ file , $ range );
59
45
60
- foreach ($ file ->getNamespaces () as $ namespace ) {
61
- foreach ($ namespace ->getClasses () as $ class ) {
62
- foreach ($ class ->getMethods () as $ method ) {
63
- if ($ method ->getStartLine () < $ lastLine && $ lastLine < $ method ->getEndLine ()) {
64
- return $ method ->getEndLine ();
65
- }
66
- }
67
- }
46
+ if ($ method === null ) {
47
+ throw new \InvalidArgumentException ("Could not find method end line. " );
68
48
}
69
49
70
- throw new \ InvalidArgumentException ( " Could not find method end line. " );
50
+ return $ method-> getEndLine ( );
71
51
}
72
52
73
53
public function getMethodStartLine (File $ file , LineRange $ range )
74
54
{
75
- $ this ->broker = new Broker (new Memory );
76
- $ file = $ this ->broker ->processString ($ file ->getCode (), $ file ->getRelativePath (), true );
77
- $ lastLine = $ range ->getEnd ();
55
+ $ method = $ this ->findMatchingMethod ($ file , $ range );
78
56
79
- foreach ($ file ->getNamespaces () as $ namespace ) {
80
- foreach ($ namespace ->getClasses () as $ class ) {
81
- foreach ($ class ->getMethods () as $ method ) {
82
- if ($ method ->getStartLine () < $ lastLine && $ lastLine < $ method ->getEndLine ()) {
83
- return $ method ->getStartLine ();
84
- }
85
- }
86
- }
57
+ if ($ method === null ) {
58
+ throw new \InvalidArgumentException ("Could not find method start line. " );
87
59
}
88
60
89
- throw new \ InvalidArgumentException ( " Could not find method start line. " );
61
+ return $ method-> getStartLine ( );
90
62
}
91
63
92
64
public function getLineOfLastPropertyDefinedInScope (File $ file , $ lastLine )
@@ -115,20 +87,7 @@ public function getLineOfLastPropertyDefinedInScope(File $file, $lastLine)
115
87
116
88
public function isInsideMethod (File $ file , LineRange $ range )
117
89
{
118
- $ this ->broker = new Broker (new Memory );
119
- $ file = $ this ->broker ->processString ($ file ->getCode (), $ file ->getRelativePath (), true );
120
-
121
- foreach ($ file ->getNamespaces () as $ namespace ) {
122
- foreach ($ namespace ->getClasses () as $ class ) {
123
- foreach ($ class ->getMethods () as $ method ) {
124
- if ($ method ->getStartLine () < $ range ->getStart () && $ range ->getEnd () < $ method ->getEndLine ()) {
125
- return true ;
126
- }
127
- }
128
- }
129
- }
130
-
131
- return false ;
90
+ return $ this ->findMatchingMethod ($ file , $ range ) !== null ;
132
91
}
133
92
134
93
/**
@@ -154,4 +113,26 @@ public function findClasses(File $file)
154
113
155
114
return $ classes ;
156
115
}
116
+
117
+ private function findMatchingMethod (File $ file , LineRange $ range )
118
+ {
119
+ $ foundMethod = null ;
120
+
121
+ $ this ->broker = new Broker (new Memory );
122
+ $ file = $ this ->broker ->processString ($ file ->getCode (), $ file ->getRelativePath (), true );
123
+ $ lastLine = $ range ->getEnd ();
124
+
125
+ foreach ($ file ->getNamespaces () as $ namespace ) {
126
+ foreach ($ namespace ->getClasses () as $ class ) {
127
+ foreach ($ class ->getMethods () as $ method ) {
128
+ if ($ method ->getStartLine () < $ lastLine && $ lastLine < $ method ->getEndLine ()) {
129
+ $ foundMethod = $ method ;
130
+ break ;
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ return $ foundMethod ;
137
+ }
157
138
}
0 commit comments