Test Failed
Branch master (d625c8)
by Joe
04:55
created

Parse::execute()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 46
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 64
ccs 0
cts 42
cp 0
crap 30
rs 8.6346

1 Method

Rating   Name   Duplication   Size   Complexity  
A do_call() 0 14 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Plugins Management
4
 * @author Joe Huss <[email protected]>
5
 * @copyright 2017
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
	protected function configure() {
30
		$this
31
			->setName('myadmin:parse') // the name of the command (the part after "bin/console")
32
			->setDescription('Parses PHP DocBlocks') // the short description shown while running "php bin/console list"
33
			->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
34
	}
35
36
	/** (optional)
37
	 * This method is executed before the interact() and the execute() methods.
38
	 * Its main purpose is to initialize variables used in the rest of the command methods.
39
	 *
40
	 * @param \Symfony\Component\Console\Input\InputInterface   $input
41
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
42
	 */
43
	protected function initialize(InputInterface $input, OutputInterface $output) {}
44
45
	/** (optional)
46
	 * This method is executed after initialize() and before execute().
47
	 * Its purpose is to check if some of the options/arguments are missing and interactively
48
	 * ask the user for those values. This is the last place where you can ask for missing
49
	 * options/arguments. After this command, missing options/arguments will result in an error.
50
	 *
51
	 * @param \Symfony\Component\Console\Input\InputInterface   $input
52
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
53
	 */
54
	protected function interact(InputInterface $input, OutputInterface $output) {}
55
56
57
	/** (required)
58
	 * This method is executed after interact() and initialize().
59
	 * It contains the logic you want the command to execute.
60
	 *
61
	 * @param InputInterface $input
62
	 * @param OutputInterface $output
63
	 */
64
	protected function execute(InputInterface $input, OutputInterface $output) {
65
		$output->writeln([ // outputs multiple lines to the console (adding "\n" at the end of each line)
66
			'MyAdmin DocBlock Parser',
67
			'=======================',
68
			''
69
		                 ]);
70
		$output->writeln('<info>foo</info>'); // green text
71
		$output->writeln('<comment>foo</comment>'); // yellow text
72
		$output->writeln('<question>foo</question>'); // black text on a cyan background
73
		$output->writeln('<error>foo</error>'); // white text on a red background
74
		$formatter = $this->getHelper('formatter');
75
		// Section - [SomeSection] Here is some message related to that section
76
		$formattedLine = $formatter->formatSection('SomeSection', 'Here is some message related to that section');
77
		$output->writeln($formattedLine);
78
		// Error Block
79
		$errorMessages = ['Error!', 'Something went wrong'];
80
		$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
81
		$output->writeln($formattedBlock);
82
83
		$paths = ['include', 'scripts'];
84
		$calls = [
85
			'getFiles'      => ['getHash', 'getSource', 'getNamespaces', 'getIncludes', 'getConstants', 'getFunctions', 'getInterfaces', 'getTraits', 'getPath', 'getDocBlock', 'getName'],
86
			'getNamespaces' => ['getClasses', 'getConstants', 'getFunctions', 'getInterfaces', 'getTraits', 'getFqsen', 'getName'],
87
			'getClasses'    => ['isFinal', 'isAbstract', 'getParent', 'getInterfaces', 'getConstants', 'getMethods', 'getProperties', 'getUsedTraits', 'getFqsen', 'getName', 'getDocBlock'],
88
			'getMethods'    => ['isAbstract', 'isFinal', 'isStatic', 'getVisibility', 'getArguments', 'getFqsen', 'getName', 'getDocBlock'],
89
			'getProperties' => ['isStatic', 'getDefault', 'getTypes', 'getVisibility', 'getFqsen', 'getName', 'getDocBlock'],
90
			'getTraits'     => ['getMethods', 'getProperties', 'getUsedTraits', 'getFqsen', 'getName', 'getDocBlock'],
91
			'getInterfaces' => ['getParents', 'getConstants', 'getMethods', 'getFqsen', 'getName', 'getDocBlock'],
92
			'getArguments'  => ['isByReference', 'isVariadic', 'getName', 'getTypes', 'getDefault'],
93
			'getFunctions'  => ['getArguments', 'getFqsen', 'getName', 'getDocBlock'],
94
			'getConstants'  => ['getValue', 'getFqsen', 'getName', 'getDocBlock']
95
		];
96
		/**
97
		 * @param $parent
98
		 * @param $call
99
		 * @param $calls
100
		 * @return array
101
		 */
102
		function do_call($parent, $call, $calls) {
103
			echo "Running \$parent->$call();".PHP_EOL;
104
			$response = $parent->$call();
105
			if (isset($calls[$call])) {
106
				$out = [];
107
				/** @var \phpDocumentor\Reflection\Php\File $file */
108
				foreach ($response as $idx => $child)
109
					foreach ($calls[$call] as $childCall)
110
						/** @var \phpDocumentor\Reflection\Php\File $file */
111
						$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...
112
						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...
113
				return $out;
114
			} else
115
				return $response;
116
		}
117
118
		$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...
119
		$files = [];
120
		$map = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $map is dead and can be removed.
Loading history...
121
		foreach ($paths as $path)
122
			$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...
123
		/** @var Project $project */
124
		$project = $projectFactory->create('MyProject', $files);
125
		$map = do_call($project, 'getFiles', $calls);
126
		file_put_contents(__DIR__.'/../../../../../include/config/parse.serial', serialize($map));
127
		file_put_contents(__DIR__.'/../../../../../include/config/parse.json', json_encode($map, JSON_PRETTY_PRINT));
128
		/** @var \phpDocumentor\Reflection\Php\Class_ $class */
129
		/* foreach ($file->getClasses() as $class)
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
			echo '- ' . $class->getFqsen() . PHP_EOL;
131
		} */
132
		/** @var \phpDocumentor\Reflection\Php\Function_ $function */
133
		/* foreach ($file->getFunctions() as $function) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
134
			echo '- ' . $function->getFqsen() . PHP_EOL;
135
		} */
136
137
		/** DocBlock / Reflection Parsing
138
		 * Reconstituting a docblock - https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/examples/03-reconstituting-a-docblock.php
139
		 * Adding Your own Tag - https://github.com/phpDocumentor/ReflectionDocBlock/blob/master/examples/04-adding-your-own-tag.php
140
		 */
141
142
		/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
143
		$class = new ReflectionClass('MyClass');
144
		$phpdoc = new \phpDocumentor\Reflection\DocBlock($class);
145
146
		var_dump($phpdoc->getShortDescription());
147
		var_dump($phpdoc->getLongDescription()->getContents());
148
		var_dump($phpdoc->getTags());
149
		var_dump($phpdoc->hasTag('author'));
150
		var_dump($phpdoc->hasTag('copyright'));
151
		// But we can also grab all tags of a specific type, such as `see`
152
		$seeTags = $docblock->getTagsByName('see');
153
154
		$reflector = new ReflectionClass('Example');
155
		// to get the Class DocBlock
156
		echo $reflector->getDocComment();
157
		// to get the Method DocBlock
158
		$reflector->getMethod('fn')->getDocComment();
159
		*/
160
	}
161
}
162