b60116db3c620dd4064c390bd1e0472e0ffc0b22
[friendica.git/.git] / src / Core / Console / CreateDoxygen.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 /**
6  * Description of CreateDoxygen
7  *
8  * @author Hypolite Petovan <mrpetovan@gmail.com>
9  */
10 class CreateDoxygen extends \Asika\SimpleConsole\Console
11 {
12         protected $helpOptions = ['h', 'help', '?'];
13
14         protected function getHelp()
15         {
16                 $help = <<<HELP
17 console createdoxygen - Generate Doxygen headers
18 Usage
19         bin/console createdoxygen <file> [-h|--help|-?] [-v]
20
21 Description
22         Outputs the provided file with added Doxygen headers to functions
23
24 Options
25     -h|--help|-? Show help information
26     -v           Show more debug information.
27 HELP;
28                 return $help;
29         }
30
31         protected function doExecute()
32         {
33                 if ($this->getOption('v')) {
34                         $this->out('Class: ' . __CLASS__);
35                         $this->out('Arguments: ' . var_export($this->args, true));
36                         $this->out('Options: ' . var_export($this->options, true));
37                 }
38
39                 if (count($this->args) == 0) {
40                         $this->out($this->getHelp());
41                         return 0;
42                 }
43
44                 if (count($this->args) > 1) {
45                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
46                 }
47
48                 $file = $this->getArgument(0);
49                 if (!file_exists($file)) {
50                         throw new \RuntimeException('Unable to find specified file.');
51                 }
52
53                 $data = file_get_contents($file);
54
55                 $lines = explode("\n", $data);
56
57                 $previous = "";
58
59                 foreach ($lines AS $line) {
60                         $line = rtrim(trim($line, "\r"));
61
62                         if (strstr(strtolower($line), "function")) {
63                                 $detect = strtolower(trim($line));
64                                 $detect = implode(" ", explode(" ", $detect));
65
66                                 $found = false;
67
68                                 if (substr($detect, 0, 9) == "function ") {
69                                         $found = true;
70                                 }
71
72                                 if (substr($detect, 0, 19) == "protected function ") {
73                                         $found = true;
74                                 }
75
76                                 if (substr($detect, 0, 17) == "private function ") {
77                                         $found = true;
78                                 }
79
80                                 if (substr($detect, 0, 23) == "public static function ") {
81                                         $found = true;
82                                 }
83
84                                 if (substr($detect, 0, 24) == "private static function ") {
85                                         $found = true;
86                                 }
87
88                                 if (substr($detect, 0, 10) == "function (") {
89                                         $found = false;
90                                 }
91
92                                 if ($found && ( trim($previous) == "*/")) {
93                                         $found = false;
94                                 }
95
96                                 if ($found) {
97                                         $this->out($this->addDocumentation($line));
98                                 }
99                         }
100                         $this->out($line);
101                         $previous = $line;
102                 }
103
104                 return 0;
105         }
106
107         /**
108          * @brief Adds a doxygen header
109          *
110          * @param string $line The current line of the document
111          *
112          * @return string added doxygen header
113          */
114         private function addDocumentation($line)
115         {
116                 $trimmed = ltrim($line);
117                 $length = strlen($line) - strlen($trimmed);
118                 $space = substr($line, 0, $length);
119
120                 $block = $space . "/**\n" .
121                         $space . " * @brief \n" .
122                         $space . " *\n"; /**/
123
124
125                 $left = strpos($line, "(");
126                 $line = substr($line, $left + 1);
127
128                 $right = strpos($line, ")");
129                 $line = trim(substr($line, 0, $right));
130
131                 if ($line != "") {
132                         $parameters = explode(",", $line);
133                         foreach ($parameters AS $parameter) {
134                                 $parameter = trim($parameter);
135                                 $splitted = explode("=", $parameter);
136
137                                 $block .= $space . " * @param " . trim($splitted[0], "& ") . "\n";
138                         }
139                         if (count($parameters) > 0) $block .= $space . " *\n";
140                 }
141
142                 $block .= $space . " * @return \n" .
143                         $space . " */\n";
144
145                 return $block;
146         }
147
148 }