AttachesPipesTrait::getWorkflowsNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Cerbero\Workflow\Console\Commands;
2
3
/**
4
 * Trait to attach pipes to pipelines.
5
 *
6
 * @author	Andrea Marco Sartori
7
 */
8
trait AttachesPipesTrait {
9
10
	/**
11
	 * @author	Andrea Marco Sartori
12
	 * @var		string	$currentPipe	Pipe to generate.
13
	 */
14
	protected $currentPipe;
15
16
	/**
17
	 * Generate the specified pipes.
18
	 *
19
	 * @author	Andrea Marco Sartori
20
	 * @return	void
21
	 */
22
	protected function generatePipes()
23
	{
24
		foreach ($this->getPipesByOption('attach') as $pipe)
25
		{
26
			$this->currentPipe = $pipe;
27
28
			parent::fire();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (fire() instead of generatePipes()). Are you sure this is correct? If so, you might want to change this to $this->fire().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
29
		}
30
	}
31
32
	/**
33
	 * Retrieve a list of pipes.
34
	 *
35
	 * @author	Andrea Marco Sartori
36
	 * @param	string  $option
37
	 * @return	array
38
	 */
39
	protected function getPipesByOption($option)
40
	{
41
		$pipes = $this->option($option);
0 ignored issues
show
Bug introduced by
The method option() does not exist on Cerbero\Workflow\Console...ands\AttachesPipesTrait. Did you maybe mean getPipesByOption()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
42
43
		preg_match_all('/\w+/', $pipes, $matches);
44
45
		return array_map('ucfirst', $matches[0]);
46
	}
47
48
	/**
49
	 * Get the default namespace for the class.
50
	 *
51
	 * @param  string  $rootNamespace
52
	 * @return string
53
	 */
54
	protected function getDefaultNamespace($rootNamespace)
55
	{
56
		$workflows = $this->getWorkflowsNamespace();
57
58
		$pipeline = $this->getWorkflowName();
0 ignored issues
show
Bug introduced by
It seems like getWorkflowName() 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...
59
60
		return "{$rootNamespace}\\{$workflows}\\{$pipeline}";
61
	}
62
63
	/**
64
	 * Retrieve the namespace of the workflows.
65
	 *
66
	 * @author	Andrea Marco Sartori
67
	 * @return	string
68
	 */
69
	protected function getWorkflowsNamespace()
70
	{
71
		$relative = ltrim(config('workflow.path'), app_path());
72
73
		$chunks = array_map('ucfirst', explode('/', $relative));
74
75
		return implode('\\', $chunks);
76
	}
77
78
	/**
79
	 * Retrieve a list of pipes with their namespaces.
80
	 *
81
	 * @author	Andrea Marco Sartori
82
	 * @param	string  $option
83
	 * @return	array
84
	 */
85
	protected function getNamespacedPipesByOption($option)
86
	{
87
		return array_map([$this, 'parseName'], $this->getPipesByOption($option));
88
	}
89
90
	/**
91
	 * Get the desired class name from the input.
92
	 *
93
	 * @return string
94
	 */
95
	protected function getNameInput()
96
	{
97
		return $this->currentPipe;
98
	}
99
100
}
101