[ create a new paste ] login | about

Link: http://codepad.org/iq5yp5NV    [ raw code | output | fork ]

k4st - PHP, pasted on Nov 29:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php

/* $Id: help.php 172 2008-07-23 15:48:01Z peter.goodman $ */

/**
 * help(mixed) -> void
 *
 * Given a function name, class name, or object, output a nicely formatted 
 * documentation string.
 *
 * @author Peter Goodman
 */
function help($thing, $method = NULL) {
    $str = '';
    
    if($method !== NULL) {
        $str = Doc::formatFunction(
            Doc::get($thing, $method),
            $method
        );
    
    } else if(is_string($thing)) {
        
        // existing function
        if(function_exists($thing)) {
            $str = Doc::formatFunction(
                Doc::get($thing),
                $thing
            );
        
        // existing class/interface
        } else if(class_exists($thing, FALSE) || interface_exists($thing, FALSE)) {
            $str = Doc::formatClass($thing);
        
        // could be a file rooted somewhere 
        } else {
            if(strpos($thing, '.') !== FALSE) {
                // todo, make it so that a file can also be introspected
            }
        }
    
    // object
    } else if(is_object($thing)) {
        $str = Doc::formatClass(get_class($thing));
    
    // unknown
    } else {
        throw new InvalidArgumentException(
            "Function [help] expects either string or object as first ".
            "parameter to be string or object. Neither given."
        );
    }
    
    if(!empty($str)) {
        echo "<pre>$str</pre>";
        flush();
    }
}


/**
 * Class that handles building up class / function document strings. It is
 * abstract because all methods are static and this is a non-instantiable
 * class.
 *
 * @author Peter Goodman
 * @internal
 */
abstract class Doc {
    
    /**
     * Doc::get(mixed $method_or_class[, string $method])__ -> string
     *
     * Gets a nicely formatted doc-block for a function/method/class/object. To
     * get the documentation for a class/method, do: 
     *     Doc::get('class name', 'methodname')
     *     Doc::get($obj, 'method name')
     *
     * @author Peter Goodman
     */
    static public function get() {

        $reflector = NULL;
        $callback = func_get_args();

        if(!isset($callback[0]))
            return '';

        try {
            // class/object method
            if(count($callback) === 2)
                $reflector = new ReflectionMethod($callback[0], $callback[1]);

            // obect
            else if(is_object($callback[0]))
                $reflector = new ReflectionObject($callback[0]);

            // function/class
            else {
                if(function_exists($callback[0]))
                    $reflector = new ReflectionFunction($callback[0]);

                else if(class_exists($callback[0], FALSE) || 
                        interface_exists($callback[0], FALSE))
                    $reflector = new ReflectionClass($callback[0]);

                else
                    return '';
            }

        } catch(Exception $e) {
            return '';
        }

        // get and return the formatted doc block
        return self::formatBlock($reflector->getDocComment());
    }
    
    /**
     * Doc::formatBlock(string) -> string
     *
     * Format a string as a doc block.
     */
    static public function formatBlock($doc_block) {
        $doc_block = preg_replace('~(\r?\n)+~', "\n", $doc_block);

        // get rid of leading and trailing multi-line comments delimiters
        $doc_block = preg_replace('~(/[*]{1,2}|[*]/)~', '', $doc_block);

        // get rid of leading comment markers (*'s)
        $doc_block = preg_replace('~(\n|\A)?(?<!\w)\s+[*][ ]?~', "\n", $doc_block);

        // replace php-doc identifiers with something readable
        $doc_block = preg_replace('~@internal~s', '', $doc_block);
        $doc_block = preg_replace('~@(\w+)~e', 'ucfirst("$1").":"', $doc_block);

        return htmlentities(trim($doc_block), ENT_QUOTES);
    }
    
    /**
     * Doc::formatFunction(string $doc_block, string $function_name) -> string
     *
     * Given a paragraph of documentation and the name of a function, return a
     * nicely formatted string.
     *
     * @internal
     */
    static public function formatFunction($doc_block, $function_name) {
        $doc_block = preg_replace('~\n~', "\n    ", "    ".trim($doc_block));
        return "<i>{$function_name}(...)</i>\n{$doc_block}";
    }
    
    /**
     * Doc::formatSection(string $doc_block, string $prefix) -> string
     *
     * Prefix each line of a documentation block.
     *
     * @internal
     */
    static protected function formatSection($doc_block, $prefix) {
        $doc_block = trim($doc_block);

        if(!empty($doc_block))
            return preg_replace('~\n~', "\n{$prefix}", "{$prefix}{$doc_block}");
        
        return '';
    }
    
    /**
     * Doc::formatConstructor(ReflectionClass) -> string
     *
     * Format the constructor of a class.
     *
     * @internal
     */
    static protected function formatConstructor(ReflectionClass $reflector) {
        try {
            $constructor = $reflector->getConstructor();
            if($constructor instanceof ReflectionMethod)
                return self::formatBlock($constructor->getDocComment());

        // ignore it
        } catch(Exception $e) {}

        return '';
    }
    
    /**
     * Doc::getClassDescendants(string $class_name, [array &$parent[, array &$classes]]) 
     * -> array
     * 
     * Return a tree of the classes/interfaces the extend/implement this class or
     * interface. If the class or interface name passed in does not exist then
     * an InvalidArgumentException will be thrown.
     *
     * @note This function is anything but efficient so use it sparingly.
     * @author Peter Goodman
     * @internal
     */
    static protected function getClassDescendants($class_name, 
                                           array &$parent = array(), 
                                           array &$classes = array()) {

        if(!class_exists($class_name, FALSE) && 
           !interface_exists($class_name, FALSE)) {
            throw new InvalidArgumentException(
                "The class/interface [{$class_name}] does not exist."
            );
        }

        // merge the list of declared classes and iterators. we only find these
        // once and them subtract from them
        if(empty($classes)) {
            $classes = array_merge(
                get_declared_interfaces(),
                get_declared_classes()
            );
        }

        // go through the defined interfaces and classes and recursively build a
        // tree of the extending classes/interfaces
        foreach($classes as $i => $class) {

            if($class === NULL)
                continue;

            // look at its parent class
            if(get_parent_class($class) == $class_name)
                $parent[$class] = array();

            // look at its parent interfaces, this won't actually get a proper
            // tree of things given that multiple interfaces can be extended/
            // implemented at once, but it will give a good idea of things
            else {
                $interfaces = class_implements($class, FALSE);
                $interfaces = array_intersect(
                    $interfaces === FALSE ? array() : $interfaces,
                    $classes
                );

                if(in_array($class_name, $interfaces))
                    $parent[$class] = array();
            }

            // get the descendants recursively
            if(isset($parent[$class])) {
                $classes[$i] = NULL;
                self::getClassDescendants($class, $parent[$class], $classes);
            }

            // sort this level's classes
            ksort($parent);
        }

        return $parent;
    }
    
    /**
     * Doc::formatClassDescendants(array $descendants[, int $level]) -> string
     *
     * Return a formatted tree of class descendants from an array (tree) of 
     * class descendants. This function builds the formatted string recursively.
     *
     * @internal
     */
    static protected function formatClassDescendants(array $descendants, 
                                                           $level = 0) {

        $str = '';
        $level = abs($level);

        // go over the descendants and build up the string
        foreach($descendants as $class => $sub_classes) {
            $str .= "\n";

            if($level > 0)
                $str .= str_repeat('    ', $level);

            $str .= $class;

            // recursively build up the sub-classes
            $str .= self::formatClassDescendants($sub_classes, $level+1);
        }

        return $str;
    }
    
    /**
     * Doc::formatClass(string $class_name) -> string
     *
     * Given a class or interface name, return a nicely formatted documentation 
     * string describing the public, private, and protected methods of that class.
     *
     * @internal
     */
    static public function formatClass($class_name) {

        // class doesn't exist
        if(!class_exists($class_name, FALSE) && 
           !interface_exists($class_name, FALSE)) {
            throw new InvalidArgumentException(
                "Class/Interface [{$class_name}] does not exist and therefore ".
                "cannot be introspected."
            );
        }

        // get the reflector
        try {
            $reflector = new ReflectionClass($class_name);
        } catch(Exception $e) {
            return '';
        }

        $is_interface = $reflector->isInterface();

        // prefixes
        $line_prefix = ' |  ';
        $section_prefix = "\n{$line_prefix}\n";
        $method_prefix = "{$line_prefix}    ";

        // the doc block for the class
        $doc_block = self::formatBlock($reflector->getDocComment());

        // can this class be extended?
        $final = $reflector->isFinal() && !$is_interface ? 'final ' : '';

        // the type of the class
        $type = $is_interface ? 'interface' : (
            $reflector->isAbstract() ? 'abstract class' : 'class'
        );

        $str = "<b>{$final}{$type} {$class_name}</b>";
        if(!empty($doc_block))
            $str .= "\n". self::formatSection($doc_block, $line_prefix);

        // show the class constants
        $constants = $reflector->getConstants();
        if(!empty($constants)) {
            $str .= "{$section_prefix}{$line_prefix}<u>Constants:</u>";

            foreach($constants as $name => $value)
                $str .= "\n{$method_prefix} {$name} -> {$value}";
        }

        // format the class constructor (if it has one)
        $doc_block = self::formatConstructor($reflector);
        if(!empty($doc_block)) {
            $str .= "{$section_prefix}{$line_prefix}<u>Constructor:</u>";
            $str .= $section_prefix. self::formatSection(
                $doc_block,
                $method_prefix
            );
        }

        // get and sort out the methods of this class
        $methods = $reflector->getMethods();
        $types = array(array(), array(), array());
        $headers = array(
            'Public Methods:', 
            'Protected Methods:', 
            'Private Methods:',
        );

        // sort the methods into {0:public, 1:protected, 2:private}
        foreach($methods as $method) {
            $type = $method->isPublic() ? 0 : ($method->isProtected() ? 1 : 2);
            $types[$type][$method->getName()] = $method;
        }

        // output the method infos
        foreach($headers as $i => $header) {
            if(empty($types[$i]))
                continue;

            $str .= "{$section_prefix}{$line_prefix}<u>{$header}</u>";

            ksort($types[$i]);
            foreach($types[$i] as $method) {

                // ignore the constructor and destructor
                if($method->isConstructor() || $method->isDestructor())
                    continue;

                // method modifiers (static, abstract)
                $modifiers = array();
                $method->isStatic() && ($modifiers[] = 'static');
                $method->isAbstract() && ($modifiers[] = 'abstract');
                if(!empty($modifiers))
                    $modifiers = '&lt;'. implode(', ', $modifiers) .'&gt;';
                else
                    $modifiers = '';

                // add in the formatted section
                $str .= $section_prefix . self::formatSection(
                    self::formatFunction(
                        self::formatBlock($method->getDocComment()),
                        $method->getName() . $modifiers
                    ),
                    $method_prefix
                );
            }
        }

        // get the extending classes
        $classes = array();
        self::getClassDescendants($class_name, $classes);

        // there are parent classes so format them
        if(!empty($classes)) {
            $str .= "{$section_prefix}{$line_prefix}<u>Class Descendants:</u>\n";
            $str .= "{$line_prefix}\n";
            $str .= self::formatSection(
                self::formatClassDescendants($classes),
                $method_prefix
            );
        }

        return $str;
    }
}

help('Doc');


Output:
<pre><b>abstract class Doc</b>
 |  Class that handles building up class / function document strings. It is
 |  abstract because all methods are static and this is a non-instantiable
 |  class.
 |  
 |  Author: Peter Goodman
 |  
 |  <u>Public Methods:</u>
 |  
 |      <i>formatBlock&lt;static&gt;(...)</i>
 |          Doc::formatBlock(string) -&gt; string
 |          
 |          Format a string as a doc block.
 |  
 |      <i>formatClass&lt;static&gt;(...)</i>
 |          Doc::formatClass(string $class_name) -&gt; string
 |          
 |          Given a class or interface name, return a nicely formatted documentation 
 |          string describing the public, private, and protected methods of that class.
 |  
 |      <i>formatFunction&lt;static&gt;(...)</i>
 |          Doc::formatFunction(string $doc_block, string $function_name) -&gt; string
 |          
 |          Given a paragraph of documentation and the name of a function, return a
 |          nicely formatted string.
 |  
 |      <i>get&lt;static&gt;(...)</i>
 |          Doc::get(mixed $method_or_class[, string $method])__ -&gt; string
 |          
 |          Gets a nicely formatted doc-block for a function/method/class/object. To
 |          get the documentation for a class/method, do:
 |              Doc::get(&#039;class name&#039;, &#039;methodname&#039;)
 |              Doc::get($obj, &#039;method name&#039;)
 |          
 |          Author: Peter Goodman
 |  
 |  <u>Protected Methods:</u>
 |  
 |      <i>formatClassDescendants&lt;static&gt;(...)</i>
 |          Doc::formatClassDescendants(array $descendants[, int $level]) -&gt; string
 |          
 |          Return a formatted tree of class descendants from an array (tree) of 
 |          class descendants. This function builds the formatted string recursively.
 |  
 |      <i>formatConstructor&lt;static&gt;(...)</i>
 |          Doc::formatConstructor(ReflectionClass) -&gt; string
 |          
 |          Format the constructor of a class.
 |  
 |      <i>formatSection&lt;static&gt;(...)</i>
 |          Doc::formatSection(string $doc_block, string $prefix) -&gt; string
 |          
 |          Prefix each line of a documentation block.
 |  
 |      <i>getClassDescendants&lt;static&gt;(...)</i>
 |          Doc::getClassDescendants(string $class_name, [array &amp;$parent[, array &amp;$classes]])
 |          -&gt; array
 |          
 |          Return a tree of the classes/interfaces the extend/implement this class or
 |          interface. If the class or interface name passed in does not exist then
 |          an InvalidArgumentException will be thrown.
 |          
 |          Note: This function is anything but efficient so use it sparingly.
 |          Author: Peter Goodman</pre>


Create a new paste based on this one


Comments: