/** * Extracts usage of method * * @param ReflectedMethod $method * @return $this */ private function extractUsage(ReflectedMethod $method) { $tokens = $method->getTokens(); $codes = $values = array(); foreach ($tokens as $token) { if (in_array($token->getType(), array(T_WHITESPACE, T_BOOL_CAST, T_INT_CAST, T_STRING_CAST, T_DOUBLE_CAST, T_OBJECT_CAST))) { continue; } array_push($codes, $token->getType()); array_push($values, $token->getValue()); } switch (true) { case preg_match('!^(get)|(is)|(has).*!', $method->getName()) && $codes == array(T_RETURN, T_VARIABLE, T_OBJECT_OPERATOR, T_STRING, T_STRING): $method->setUsage(MethodUsage::USAGE_GETTER); break; // basic setter // basic setter case preg_match('!^set.*!', $method->getName()) && $codes == array(T_VARIABLE, T_OBJECT_OPERATOR, T_STRING, T_STRING, T_VARIABLE, T_STRING) && $values[3] == '=': // fluent setter // fluent setter case preg_match('!^set.*!', $method->getName()) && $codes == array(T_VARIABLE, T_OBJECT_OPERATOR, T_STRING, T_STRING, T_VARIABLE, T_STRING, T_RETURN, T_VARIABLE, T_STRING) && $values[3] == '=' && $values[7] == '$this': $method->setUsage(MethodUsage::USAGE_SETTER); break; default: $method->setUsage(MethodUsage::USAGE_UNKNWON); } return $this; }
/** * Attach method * This method consolidated dependencies * * @param ReflectedMethod $method * @return $this */ public function pushMethod(ReflectedMethod $method) { $this->methods[$method->getName()] = $method; return $this; }
/** * Search methods whose are called or call this method * * @param ReflectedClass $class * @param ReflectedMethod $method * @return array */ private function searchDirectLinkedMethodsByCall(ReflectedClass $class, ReflectedMethod $method) { $linked = array(); // A calls B if (preg_match_all('!\\$this\\->(\\w*?)\\(!im', $method->getContent(), $matches)) { list(, $linked) = $matches; } // B calls A foreach ($class->getMethods() as $otherMethod) { $otherCalls = array(); if (preg_match_all('!\\$this\\->(\\w*?)\\(!im', $otherMethod->getContent(), $matches)) { list(, $otherCalls) = $matches; } if (in_array($method->getName(), $otherCalls)) { array_push($linked, $otherMethod->getName()); } } return $linked; }