]> git.mxchange.org Git - friendica.git/blob - src/Console/CreateDoxygen.php
Inverted condition
[friendica.git] / src / Console / CreateDoxygen.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 /**
25  * Description of CreateDoxygen
26  */
27 class CreateDoxygen extends \Asika\SimpleConsole\Console
28 {
29         protected $helpOptions = ['h', 'help', '?'];
30
31         protected function getHelp()
32         {
33                 $help = <<<HELP
34 console createdoxygen - Generate Doxygen headers
35 Usage
36         bin/console createdoxygen <file> [-h|--help|-?] [-v]
37
38 Description
39         Outputs the provided file with added Doxygen headers to functions
40
41 Options
42     -h|--help|-? Show help information
43     -v           Show more debug information.
44 HELP;
45                 return $help;
46         }
47
48         protected function doExecute()
49         {
50                 if ($this->getOption('v')) {
51                         $this->out('Class: ' . __CLASS__);
52                         $this->out('Arguments: ' . var_export($this->args, true));
53                         $this->out('Options: ' . var_export($this->options, true));
54                 }
55
56                 if (count($this->args) == 0) {
57                         $this->out($this->getHelp());
58                         return 0;
59                 }
60
61                 if (count($this->args) > 1) {
62                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
63                 }
64
65                 $file = $this->getArgument(0);
66                 if (!file_exists($file)) {
67                         throw new \RuntimeException('Unable to find specified file.');
68                 }
69
70                 $data = file_get_contents($file);
71
72                 $lines = explode("\n", $data);
73
74                 $previous = "";
75
76                 foreach ($lines AS $line) {
77                         $line = rtrim(trim($line, "\r"));
78
79                         if (strstr(strtolower($line), "function")) {
80                                 $detect = strtolower(trim($line));
81                                 $detect = implode(" ", explode(" ", $detect));
82
83                                 $found = false;
84
85                                 if (substr($detect, 0, 9) == "function ") {
86                                         $found = true;
87                                 }
88
89                                 if (substr($detect, 0, 19) == "protected function ") {
90                                         $found = true;
91                                 }
92
93                                 if (substr($detect, 0, 17) == "private function ") {
94                                         $found = true;
95                                 }
96
97                                 if (substr($detect, 0, 23) == "public static function ") {
98                                         $found = true;
99                                 }
100
101                                 if (substr($detect, 0, 24) == "private static function ") {
102                                         $found = true;
103                                 }
104
105                                 if (substr($detect, 0, 10) == "function (") {
106                                         $found = false;
107                                 }
108
109                                 if ($found && ( trim($previous) == "*/")) {
110                                         $found = false;
111                                 }
112
113                                 if ($found) {
114                                         $this->out($this->addDocumentation($line));
115                                 }
116                         }
117                         $this->out($line);
118                         $previous = $line;
119                 }
120
121                 return 0;
122         }
123
124         /**
125          * Adds a doxygen header
126          *
127          * @param string $line The current line of the document
128          *
129          * @return string added doxygen header
130          */
131         private function addDocumentation($line)
132         {
133                 $trimmed = ltrim($line);
134                 $length = strlen($line) - strlen($trimmed);
135                 $space = substr($line, 0, $length);
136
137                 $block = $space . "/**\n" .
138                         $space . " * \n" .
139                         $space . " *\n"; /**/
140
141
142                 $left = strpos($line, "(");
143                 $line = substr($line, $left + 1);
144
145                 $right = strpos($line, ")");
146                 $line = trim(substr($line, 0, $right));
147
148                 if ($line != "") {
149                         $parameters = explode(",", $line);
150                         foreach ($parameters AS $parameter) {
151                                 $parameter = trim($parameter);
152                                 $splitted = explode("=", $parameter);
153
154                                 $block .= $space . " * @param " . trim($splitted[0], "& ") . "\n";
155                         }
156                         if (count($parameters) > 0) $block .= $space . " *\n";
157                 }
158
159                 $block .= $space . " * @return \n" .
160                         $space . " */\n";
161
162                 return $block;
163         }
164
165 }