@@ -34,59 +34,31 @@ public function __construct()
3434
3535 public function isMethodStatic (File $ file , LineRange $ range )
3636 {
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 );
5038
51- return false ;
39+ return $ method ? $ method -> isStatic () : false ;
5240 }
5341
5442 public function getMethodEndLine (File $ file , LineRange $ range )
5543 {
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 );
5945
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. " );
6848 }
6949
70- throw new \ InvalidArgumentException ( " Could not find method end line. " );
50+ return $ method-> getEndLine ( );
7151 }
7252
7353 public function getMethodStartLine (File $ file , LineRange $ range )
7454 {
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 );
7856
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. " );
8759 }
8860
89- throw new \ InvalidArgumentException ( " Could not find method start line. " );
61+ return $ method-> getStartLine ( );
9062 }
9163
9264 public function getLineOfLastPropertyDefinedInScope (File $ file , $ lastLine )
@@ -115,20 +87,7 @@ public function getLineOfLastPropertyDefinedInScope(File $file, $lastLine)
11587
11688 public function isInsideMethod (File $ file , LineRange $ range )
11789 {
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 ;
13291 }
13392
13493 /**
@@ -154,4 +113,26 @@ public function findClasses(File $file)
154113
155114 return $ classes ;
156115 }
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+ }
157138}
0 commit comments