1: <?php
2:
3: /*
4: * The MIT License (MIT)
5: *
6: * Copyright (c) 2013 Jonathan Vollebregt (jnvsor@gmail.com), Rokas Šleinius (raveren@gmail.com)
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy of
9: * this software and associated documentation files (the "Software"), to deal in
10: * the Software without restriction, including without limitation the rights to
11: * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12: * the Software, and to permit persons to whom the Software is furnished to do so,
13: * subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in all
16: * copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24: */
25:
26: namespace Kint\Parser;
27:
28: use Kint\Object\BasicObject;
29: use Kint\Object\InstanceObject;
30: use Kint\Object\MethodObject;
31: use Kint\Object\Representation\Representation;
32: use ReflectionClass;
33:
34: class ClassMethodsPlugin extends Plugin
35: {
36: private static $cache = array();
37:
38: public function getTypes()
39: {
40: return array('object');
41: }
42:
43: public function getTriggers()
44: {
45: return Parser::TRIGGER_SUCCESS;
46: }
47:
48: public function parse(&$var, BasicObject &$o, $trigger)
49: {
50: $class = \get_class($var);
51:
52: // assuming class definition will not change inside one request
53: if (!isset(self::$cache[$class])) {
54: $methods = array();
55:
56: $reflection = new ReflectionClass($class);
57:
58: foreach ($reflection->getMethods() as $method) {
59: $methods[] = new MethodObject($method);
60: }
61:
62: \usort($methods, array('Kint\\Parser\\ClassMethodsPlugin', 'sort'));
63:
64: self::$cache[$class] = $methods;
65: }
66:
67: if (!empty(self::$cache[$class])) {
68: $rep = new Representation('Available methods', 'methods');
69:
70: // Can't cache access paths
71: foreach (self::$cache[$class] as $m) {
72: $method = clone $m;
73: $method->depth = $o->depth + 1;
74:
75: if (!$this->parser->childHasPath($o, $method)) {
76: $method->access_path = null;
77: } else {
78: $method->setAccessPathFrom($o);
79: }
80:
81: if ($method->owner_class !== $class && $ds = $method->getRepresentation('docstring')) {
82: $ds = clone $ds;
83: $ds->class = $method->owner_class;
84: $method->replaceRepresentation($ds);
85: }
86:
87: $rep->contents[] = $method;
88: }
89:
90: $o->addRepresentation($rep);
91: }
92: }
93:
94: private static function sort(MethodObject $a, MethodObject $b)
95: {
96: $sort = ((int) $a->static) - ((int) $b->static);
97: if ($sort) {
98: return $sort;
99: }
100:
101: $sort = BasicObject::sortByAccess($a, $b);
102: if ($sort) {
103: return $sort;
104: }
105:
106: $sort = InstanceObject::sortByHierarchy($a->owner_class, $b->owner_class);
107: if ($sort) {
108: return $sort;
109: }
110:
111: return $a->startline - $b->startline;
112: }
113: }
114: