Completed
Pull Request — master (#49)
by
unknown
01:21
created

CreatePivotRelationsAction::createRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 7
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Actions\Relation;
4
5
use ReflectionClass;
6
use Config;
7
8
use Mtolhuys\LaravelSchematics\Actions\Relation\CreateRelationAction;
9
10
class CreatePivotRelationsAction extends CreateRelationAction
11
{
12
    public function execute($request)
13
    {		
14
        $stub = __DIR__ . '/../../../resources/stubs/relation.stub';
15
		
16
		$this->createRelation(
17
			$stub, 
18
			$request['pivot']['source'], 
19
			$request['pivot']['target'],
20
			" , '" . $request['pivot']['foreignKeys']['second'] . "' , '" . $request['pivot']['foreignKeys']['first'] . "'",	
21
			$request['pivot']['methods']['first'],
22
			'BelongsToMany',
23
			Config::get('schematics.model.namespace') . $request['name']
24
		);
25
26
		$this->createRelation(
27
			$stub, 
28
			$request['pivot']['target'], 
29
			$request['pivot']['source'],
30
			" , '" . $request['pivot']['foreignKeys']['first'] . "' , '" . $request['pivot']['foreignKeys']['second'] . "'",	
31
			$request['pivot']['methods']['second'],
32
			'BelongsToMany',
33
			Config::get('schematics.model.namespace') . $request['name']
34
		);
35
	}
36
	
37
	//TODO: should be place in parent so it can also be used there
38
	private function createRelation($stub, $source, $target, $keys, $methodName, $type, $pivotModelName = null)
39
	{
40
		$file = (new ReflectionClass($source))->getFileName();
41
        $lines = file($file, FILE_IGNORE_NEW_LINES);
42
		$injectionLine = $this->endOfClass($file) - 1;
43
		$pivotTable = App($pivotModelName)->getTable();
0 ignored issues
show
Unused Code introduced by
$pivotTable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
		$generateMethodParams = [
45
			'type' => $type,
46
			'source' => $source,
47
			'target' => $target,
48
			'keys'=> $keys,
49
			'method' => ['name' => $methodName],
50
			'pivot' => $pivotModelName,
51
		];
52
        $lines[$injectionLine] = PHP_EOL . $this->generateMethod($generateMethodParams, $stub) . '}';
53
		file_put_contents($file, implode("\n", $lines));
54
	}
55
}
56