Completed
Push — master ( 62549b...430ab9 )
by Nazar
04:10
created

CLI   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 90
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B print_cli_structure() 0 29 5
C print_cli_structure_internal() 0 26 11
B print_cli_structure_normalize_result() 0 20 9
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\App\Router;
9
use
10
	cli\Table,
11
	cs\Config,
12
	cs\Config\Module_Properties,
13
	cs\Page;
14
15
trait CLI {
16
	protected function print_cli_structure ($path) {
17
		$Config = Config::instance();
18
		$result = [];
19
		foreach ($Config->components['modules'] as $module_name => $data) {
20
			if ($data['active'] == Module_Properties::ENABLED) {
21
				$working_dir = MODULES."/$module_name/cli";
22
				$structure   = file_exists("$working_dir/index.json") ? file_get_json("$working_dir/index.json") : [];
23
				$this->print_cli_structure_internal($working_dir, $module_name, '', $structure, $result[$module_name]);
24
			}
25
		}
26
		$result = $this->print_cli_structure_normalize_result($result);
27
		$Page   = Page::instance();
28
		// Cut `/cli/` prefix
29
		$path = substr($path, 5);
30
		if ($path) {
31
			$Page->content("%yPaths and methods for \"$path\":%n\n");
32
			$result = array_filter(
33
				$result,
34
				function ($item) use ($path) {
35
					return strpos($item[0], $path) === 0;
36
				}
37
			);
38
		} else {
39
			$Page->content("%yAll paths and methods:%n\n");
40
		}
41
		$Page->content(
42
			implode("\n", (new Table(['Path', 'Methods available'], $result))->getDisplayLines())."\n"
43
		);
44
	}
45
	/**
46
	 * @param string $dir
47
	 * @param string $module_name
48
	 * @param string $basename
49
	 * @param array  $structure
50
	 * @param array  $result
51
	 */
52
	protected function print_cli_structure_internal ($dir, $module_name, $basename, $structure, &$result) {
53
		/** @noinspection NestedTernaryOperatorInspection */
54
		foreach ($structure ?: (!$basename ? ['index'] : []) as $path => $nested_structure) {
55
			if (!is_array($nested_structure)) {
56
				$path             = $nested_structure;
57
				$nested_structure = [];
58
			}
59
			$key = $path == '_' ? 0 : $path;
60
			if (file_exists("$dir/Controller.php")) {
61
				$result[$key] = $this->controller_router_available_methods(
1 ignored issue
show
Bug introduced by
It seems like controller_router_available_methods() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
					$dir,
63
					"\\cs\\modules\\$module_name\\cli\\Controller",
64
					$basename ? $basename.'_'.$path : $path
65
				);
66
				$new_dir      = $dir;
67
				$new_basename = $basename ? $basename.'_'.$path : $path;
68
			} else {
69
				$result[$key] = $this->files_router_available_methods($dir, $path);
1 ignored issue
show
Bug introduced by
It seems like files_router_available_methods() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
70
				$new_dir      = "$dir/$path";
71
				$new_basename = $basename;
72
			}
73
			if ($structure && $nested_structure) {
74
				$this->print_cli_structure_internal($new_dir, $module_name, $new_basename, $nested_structure, $result[$key]);
75
			}
76
		}
77
	}
78
	/**
79
	 * @param array  $result
80
	 * @param string $prefix
81
	 *
82
	 * @return string[]
83
	 */
84
	protected function print_cli_structure_normalize_result ($result, $prefix = '') {
85
		$normalized = [];
86
		foreach ($result as $key => $value) {
87
			if (is_array_assoc($value)) {
88
				if (!$prefix && isset($value['index'])) {
89
					$value[0] = $value['index'];
90
					unset($value['index']);
91
				}
92
				if (is_array(@$value[0]) && $value[0]) {
93
					$normalized[] = [$prefix.$key, strtolower(implode(', ', $value[0]))];
94
				}
95
				unset($value[0]);
96
				/** @noinspection SlowArrayOperationsInLoopInspection */
97
				$normalized = array_merge($normalized, $this->print_cli_structure_normalize_result($value, $prefix.$key.'/'));
98
			} elseif (is_array($value) && $value) {
99
				$normalized[] = [$prefix.$key, strtolower(implode(', ', $value))];
100
			}
101
		}
102
		return $normalized;
103
	}
104
}
105