Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / src / Psy / Command / ListCommand / TraitEnumerator.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Command\ListCommand;
13
14 use Symfony\Component\Console\Input\InputInterface;
15
16 /**
17  * Trait Enumerator class.
18  */
19 class TraitEnumerator extends Enumerator
20 {
21     /**
22      * {@inheritdoc}
23      */
24     protected function listItems(InputInterface $input, \Reflector $reflector = null, $target = null)
25     {
26         // bail early if current PHP doesn't know about traits.
27         if (!function_exists('trait_exists')) {
28             return;
29         }
30
31         // only list traits when no Reflector is present.
32         //
33         // @todo make a NamespaceReflector and pass that in for commands like:
34         //
35         //     ls --traits Foo
36         //
37         // ... for listing traits in the Foo namespace
38
39         if ($reflector !== null || $target !== null) {
40             return;
41         }
42
43         // only list traits if we are specifically asked
44         if (!$input->getOption('traits')) {
45             return;
46         }
47
48         $traits = $this->prepareTraits(get_declared_traits());
49
50         if (empty($traits)) {
51             return;
52         }
53
54         return array(
55             'Traits' => $traits,
56         );
57     }
58
59     /**
60      * Prepare formatted trait array.
61      *
62      * @param array $traits
63      *
64      * @return array
65      */
66     protected function prepareTraits(array $traits)
67     {
68         natcasesort($traits);
69
70         // My kingdom for a generator.
71         $ret = array();
72
73         foreach ($traits as $name) {
74             if ($this->showItem($name)) {
75                 $ret[$name] = array(
76                     'name'  => $name,
77                     'style' => self::IS_CLASS,
78                     'value' => $this->presentSignature($name),
79                 );
80             }
81         }
82
83         return $ret;
84     }
85 }