Parse::interact()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Plugins Management
4
 * @author Joe Huss <[email protected]>
5
 * @copyright 2019
6
 * @package MyAdmin
7
 * @category Plugins
8
 */
9
10
namespace MyAdmin\Plugins\Command;
11
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Composer\Command\BaseCommand;
15
16
/**
17
 * Code Parser Comand
18
 *
19
 * Coloring - http://symfony.com/doc/current/console/coloring.html
20
 * Formatting - http://symfony.com/doc/current/components/console/helpers/formatterhelper.html
21
 * Table - http://symfony.com/doc/current/components/console/helpers/table.html
22
 * Style - http://symfony.com/doc/current/console/style.html
23
 * Process Helper - http://symfony.com/doc/current/components/console/helpers/processhelper.html
24
 * Progress Bar - http://symfony.com/doc/current/components/console/helpers/progressbar.html
25
 * Question Helper - http://symfony.com/doc/current/components/console/helpers/questionhelper.html
26
 *
27
 */
28
class Parse extends BaseCommand
29
{
30
	protected function configure()
31
	{
32
		$this
33
			->setName('myadmin:parse') // the name of the command (the part after "bin/console")
34
			->setDescription('Parses PHP DocBlocks') // the short description shown while running "php bin/console list"
35
			->setHelp('This command parses your PHP Files and getting the PHP DocBlocks for each of the functions, classes, methods, etc..    It Parses meaningful information out from them and generates data on available calls including help  text, arguments, etc..'); // the full command description shown when running the command with the "--help" option
36
	}
37
38
	/** (optional)
39
	 * This method is executed before the interact() and the execute() methods.
40
	 * Its main purpose is to initialize variables used in the rest of the command methods.
41
	 *
42
	 * @param \Symfony\Component\Console\Input\InputInterface   $input
43
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
44
	 */
45
	protected function initialize(InputInterface $input, OutputInterface $output)
46
	{
47
	}
48
49
	/** (optional)
50
	 * This method is executed after initialize() and before execute().
51
	 * Its purpose is to check if some of the options/arguments are missing and interactively
52
	 * ask the user for those values. This is the last place where you can ask for missing
53
	 * options/arguments. After this command, missing options/arguments will result in an error.
54
	 *
55
	 * @param \Symfony\Component\Console\Input\InputInterface   $input
56
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
57
	 */
58
	protected function interact(InputInterface $input, OutputInterface $output)
59
	{
60
	}
61
62
63
	/** (required)
64
	 * This method is executed after interact() and initialize().
65
	 * It contains the logic you want the command to execute.
66
	 *
67
	 * @param InputInterface $input
68
	 * @param OutputInterface $output
69
	 */
70
	protected function execute(InputInterface $input, OutputInterface $output)
71
	{
72
		$output->writeln([ // outputs multiple lines to the console (adding "\n" at the end of each line)
73
			'MyAdmin DocBlock Parser',
74
			'=======================',
75
			''
76
						 ]);
77
		$output->writeln('<info>foo</info>'); // green text
78
		$output->writeln('<comment>foo</comment>'); // yellow text
79
		$output->writeln('<question>foo</question>'); // black text on a cyan background
80
		$output->writeln('<error>foo</error>'); // white text on a red background
81
		$formatter = $this->getHelper('formatter');
82
		// Section - [SomeSection] Here is some message related to that section
83
		$formattedLine = $formatter->formatSection('SomeSection', 'Here is some message related to that section');
84
		$output->writeln($formattedLine);
85
		// Error Block
86
		$errorMessages = ['Error!', 'Something went wrong'];
87
		$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
88
		$output->writeln($formattedBlock);
89
90
		$paths = ['include', 'scripts'];
91
		$calls = [
92
			'getFiles'      => ['getHash', 'getSource', 'getNamespaces', 'getIncludes', 'getConstants', 'getFunctions', 'getInterfaces', 'getTraits', 'getPath', 'getDocBlock', 'getName'],
93
			'getNamespaces' => ['getClasses', 'getConstants', 'getFunctions', 'getInterfaces', 'getTraits', 'getFqsen', 'getName'],
94
			'getClasses'    => ['isFinal', 'isAbstract', 'getParent', 'getInterfaces', 'getConstants', 'getMethods', 'getProperties', 'getUsedTraits', 'getFqsen', 'getName', 'getDocBlock'],
95
			'getMethods'    => ['isAbstract', 'isFinal', 'isStatic', 'getVisibility', 'getArguments', 'getFqsen', 'getName', 'getDocBlock'],
96
			'getProperties' => ['isStatic', 'getDefault', 'getTypes', 'getVisibility', 'getFqsen', 'getName', 'getDocBlock'],
97
			'getTraits'     => ['getMethods', 'getProperties', 'getUsedTraits', 'getFqsen', 'getName', 'getDocBlock'],
98
			'getInterfaces' => ['getParents', 'getConstants', 'getMethods', 'getFqsen', 'getName', 'getDocBlock'],
99
			'getArguments'  => ['isByReference', 'isVariadic', 'getName', 'getTypes', 'getDefault'],
100
			'getFunctions'  => ['getArguments', 'getFqsen', 'getName', 'getDocBlock'],
101
			'getConstants'  => ['getValue', 'getFqsen', 'getName', 'getDocBlock']
102
		];
103
		/**
104
		 * @param $parent
105
		 * @param $call
106
		 * @param $calls
107
		 * @return array
108
		 */
109
		function do_call($parent, $call, $calls)
110
		{
111
			echo "Running \$parent->$call();".PHP_EOL;
112
			$response = $parent->$call();
113
			if (isset($calls[$call])) {
114
				$out = [];
115
				/** @var \phpDocumentor\Reflection\Php\File $file */
116
				foreach ($response as $idx => $child) {
117
					foreach ($calls[$call] as $childCall) {
118
						/** @var \phpDocumentor\Reflection\Php\File $file */
119
						$out[$idx] = $childResponse = $child->$childCall();
0 ignored issues
show
Unused Code introduced by
The assignment to $childResponse is dead and can be removed.
Loading history...
120
					}
121
				}
122
				echo "Running \$child->$childCall();".PHP_EOL;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $childCall does not seem to be defined for all execution paths leading up to this point.
Loading history...
123
				return $out;
124
			} else {
125
				return $response;
126
			}
127
		}
128
129
		$projectFactory = \phpDocumentor\Reflection\Php\ProjectFactory::createInstance();
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\Php\ProjectFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
130
		$files = [];
131
		$map = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $map is dead and can be removed.
Loading history...
132
		foreach ($paths as $path) {
133
			$files[] = new \phpDocumentor\Reflection\File\LocalFile(__DIR__.'/../../../../../'.$path);
0 ignored issues
show
Bug introduced by
The type phpDocumentor\Reflection\File\LocalFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
134
		}
135
		/** @var Project $project */
136
		$project = $projectFactory->create('MyProject', $files);
137
		$map = do_call($project, 'getFiles', $calls);
138
		file_put_contents(__DIR__.'/../../../../../include/config/parse.serial', serialize($map));
139
		file_put_contents(__DIR__.'/../../../../../include/config/parse.json', json_encode($map, JSON_PRETTY_PRINT));
140
		/** @var \phpDocumentor\Reflection\Php\Class_ $class */
141
		/* foreach ($file->getClasses() as $class)
142
			echo '- ' . $class->getFqsen() . PHP_EOL;
143
		} */
144
		/** @var \phpDocumentor\Reflection\Php\Function_ $function */
145
		/* foreach ($file->getFunctions() as $function) {
146
			echo '- ' . $function->getFqsen() . PHP_EOL;
147
		} */
148
149
		/** DocBlock / Reflection Parsing
150
		 * Reconstituting a docblock - https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/examples/03-reconstituting-a-docblock.php
151
		 * Adding Your own Tag - https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/examples/04-adding-your-own-tag.php
152
		 */
153
154
		/*
155
		$class = new ReflectionClass('MyClass');
156
		$phpdoc = new \phpDocumentor\Reflection\DocBlock($class);
157
158
		var_dump($phpdoc->getShortDescription());
159
		var_dump($phpdoc->getLongDescription()->getContents());
160
		var_dump($phpdoc->getTags());
161
		var_dump($phpdoc->hasTag('author'));
162
		var_dump($phpdoc->hasTag('copyright'));
163
		// But we can also grab all tags of a specific type, such as `see`
164
		$seeTags = $docblock->getTagsByName('see');
165
166
		$reflector = new ReflectionClass('Example');
167
		// to get the Class DocBlock
168
		echo $reflector->getDocComment();
169
		// to get the Method DocBlock
170
		$reflector->getMethod('fn')->getDocComment();
171
		*/
172
	}
173
}
174