Overview

Namespaces

  • Pharborist
    • Constants
    • ControlStructures
    • Exceptions
    • Functions
    • Generators
    • Namespaces
    • Objects
    • Operators
    • Types
    • Variables

Classes

  • Pharborist\Variables\CompoundVariableNode
  • Pharborist\Variables\GlobalStatementNode
  • Pharborist\Variables\ReferenceVariableNode
  • Pharborist\Variables\StaticVariableNode
  • Pharborist\Variables\StaticVariableStatementNode
  • Pharborist\Variables\VariableNode
  • Pharborist\Variables\VariableVariableNode

Interfaces

  • Pharborist\Variables\VariableExpressionNode
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Pharborist;
  3: 
  4: use Pharborist\Functions\FunctionCallNode;
  5: use Pharborist\Functions\FunctionDeclarationNode;
  6: use Pharborist\Namespaces\NameNode;
  7: use Pharborist\Objects\ClassMethodCallNode;
  8: use Pharborist\Objects\ClassNode;
  9: 
 10: /**
 11:  * Factory for creating common callback filters.
 12:  */
 13: class Filter {
 14:   /**
 15:    * Callback returns true if any of the callbacks pass.
 16:    *
 17:    * @param callable[] $filters
 18:    * @return callable
 19:    */
 20:   public static function any($filters) {
 21:     return function ($node) use ($filters) {
 22:       foreach ($filters as $filter) {
 23:         if ($filter($node)) {
 24:           return TRUE;
 25:         }
 26:       }
 27:       return FALSE;
 28:     };
 29:   }
 30: 
 31:   /**
 32:    * Callback returns true if all of the callbacks pass.
 33:    *
 34:    * @param callable[] $filters
 35:    * @return callable
 36:    */
 37:   public static function all($filters) {
 38:     return function ($node) use ($filters) {
 39:       foreach ($filters as $filter) {
 40:         if (!$filter($node)) {
 41:           return FALSE;
 42:         }
 43:       }
 44:       return TRUE;
 45:     };
 46:   }
 47: 
 48:   /**
 49:    * Callback to filter for nodes of certain types.
 50:    *
 51:    * @param string $class_name ...
 52:    *  At least one fully-qualified Pharborist node type to search for.
 53:    *
 54:    * @return callable
 55:    */
 56:   public static function isInstanceOf($class_name) {
 57:     $classes = func_get_args();
 58: 
 59:     return function ($node) use ($classes) {
 60:       foreach ($classes as $class) {
 61:         if ($node instanceof $class) {
 62:           return TRUE;
 63:         }
 64:       }
 65:       return FALSE;
 66:     };
 67:   }
 68: 
 69:   /**
 70:    * Callback to filter for specific function declaration.
 71:    *
 72:    * @param string $function_name ...
 73:    *  At least one function name to search for.
 74:    *
 75:    * @return callable
 76:    */
 77:   public static function isFunction($function_name) {
 78:     $function_names = func_get_args();
 79: 
 80:     return function ($node) use ($function_names) {
 81:       if ($node instanceof FunctionDeclarationNode) {
 82:         return in_array($node->getName()->getText(), $function_names, TRUE);
 83:       }
 84:       return FALSE;
 85:     };
 86:   }
 87: 
 88:   /**
 89:    * Callback to filter for calls to a function.
 90:    *
 91:    * @param string $function_name ...
 92:    *  At least one function name to search for.
 93:    *
 94:    * @return callable
 95:    */
 96:   public static function isFunctionCall($function_name) {
 97:     $function_names = func_get_args();
 98: 
 99:     return function ($node) use ($function_names) {
100:       if ($node instanceof FunctionCallNode) {
101:         return in_array($node->getName()->getText(), $function_names, TRUE);
102:       }
103:       return FALSE;
104:     };
105:   }
106: 
107:   /**
108:    * Callback to filter for specific class declaration.
109:    *
110:    * @param string $class_name ...
111:    *  At least one class name to search for.
112:    *
113:    * @return callable
114:    */
115:   public static function isClass($class_name) {
116:     $class_names = func_get_args();
117: 
118:     return function ($node) use ($class_names) {
119:       if ($node instanceof ClassNode) {
120:         return in_array($node->getName()->getText(), $class_names, TRUE);
121:       }
122:       return FALSE;
123:     };
124:   }
125: 
126:   /**
127:    * Callback to filter for calls to a class method.
128:    *
129:    * @param string $class_name
130:    *   Fully qualified class name or expression string.
131:    * @param string $method_name
132:    *   Method name or expression string.
133:    * @return callable
134:    *   Filter callable.
135:    */
136:   public static function isClassMethodCall($class_name, $method_name) {
137:     return function ($node) use ($class_name, $method_name) {
138:       if ($node instanceof ClassMethodCallNode) {
139:         $call_class_name_node = $node->getClassName();
140:         $call_class_name = $call_class_name_node instanceof NameNode ? $call_class_name_node->getAbsolutePath() : $call_class_name_node->getText();
141:         $class_matches = $call_class_name === $class_name;
142:         $method_matches = $node->getMethodName()->getText() === $method_name;
143:         return $class_matches && $method_matches;
144:       }
145:       return FALSE;
146:     };
147:   }
148: 
149:   /**
150:    * Callback to filter comments.
151:    * @param bool $include_doc_comment
152:    * @return callable
153:    */
154:   public static function isComment($include_doc_comment = TRUE) {
155:     if ($include_doc_comment) {
156:       return function ($node) {
157:         if ($node instanceof LineCommentBlockNode) {
158:           return TRUE;
159:         }
160:         elseif ($node instanceof CommentNode) {
161:           return !($node->parent() instanceof LineCommentBlockNode);
162:         }
163:         else {
164:           return FALSE;
165:         }
166:       };
167:     }
168:     else {
169:       return function ($node) {
170:         if ($node instanceof LineCommentBlockNode) {
171:           return TRUE;
172:         }
173:         elseif ($node instanceof DocCommentNode) {
174:           return FALSE;
175:         }
176:         elseif ($node instanceof CommentNode) {
177:           return !($node->parent() instanceof LineCommentBlockNode);
178:         }
179:         else {
180:           return FALSE;
181:         }
182:       };
183:     }
184:   }
185: 
186:   /**
187:    * Callback to test if match to given node.
188:    *
189:    * @param Node $match
190:    *
191:    * @return callable
192:    */
193:   public static function is(Node $match) {
194:     return function ($node) use ($match) {
195:       return $node === $match;
196:     };
197:   }
198: 
199:   /**
200:    * Callback to test if match to given token type.
201:    *
202:    * @param int|string $type
203:    *   Token type.
204:    *
205:    * @return callable
206:    */
207:   public static function isTokenType($type) {
208:     $types = func_get_args();
209:     return function ($node) use ($types) {
210:       return $node instanceof TokenNode && in_array($node->getType(), $types);
211:     };
212:   }
213: 
214:   /**
215:    * Callback to skip whitespace and comments.
216:    *
217:    * @return callable
218:    */
219:   public static function isNotHidden() {
220:     return function ($node) {
221:       return !($node instanceof WhitespaceNode || $node instanceof CommentNode || $node instanceof LineCommentBlockNode);
222:     };
223:   }
224: 
225:   /**
226:    * Callback to match whitespace containing newlines.
227:    *
228:    * @return callable
229:    */
230:   public static function isNewline() {
231:     static $callback = NULL;
232:     if (!$callback) {
233:       $callback = function (Node $node) {
234:         return $node instanceof WhitespaceNode && $node->getNewlineCount() > 0;
235:       };
236:     }
237:     return $callback;
238:   }
239: }
240: 
Pharborist API documentation generated by ApiGen