TLink::throwHierarchyLookupException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
ccs 0
cts 6
cp 0
cc 1
eloc 8
nc 1
nop 3
crap 2
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\Traits;
10
11
use Nette;
12
use Nette\Application\UI\InvalidLinkException;
13
use Nette\Application\UI\Presenter;
14
use Ublaboo\DataGrid\DataGrid;
15
use Ublaboo\DataGrid\Exception\DataGridHasToBeAttachedToPresenterComponentException;
16
use Ublaboo\DataGrid\Exception\DataGridLinkCreationException;
17
18 1
trait TLink
19
{
20
21
	/**
22
	 * Create link to custom destination
23
	 * @param  DataGrid $grid
24
	 * @param  string   $href
25
	 * @param  array    $params
26
	 * @return string
27
	 * @throws DataGridHasToBeAttachedToPresenterComponentException
28
	 * @throws \InvalidArgumentException
29
	 * @throws DataGridLinkCreationException
30
	 */
31
	protected function createLink(DataGrid $grid, $href, $params)
32
	{
33 1
		$targetComponent = $grid;
34
35 1
		if (strpos($href, ':') !== false) {
36
			return $grid->getPresenter()->link($href, $params);
37
		}
38
39 1
		for ($iteration = 0; $iteration < 10; $iteration++) {
40 1
			$targetComponent = $targetComponent->getParent();
41
42 1
			if ($targetComponent === null) {
43
				$this->throwHierarchyLookupException($grid, $href, $params);
44
			}
45
46
			try {
47 1
				@$link = $targetComponent->link($href, $params);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
48
49
			} catch (InvalidLinkException $e) {
0 ignored issues
show
Bug introduced by
The class Nette\Application\UI\InvalidLinkException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
50
				$link = false;
51
			} catch (Nette\InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Nette\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
52
				$link = false;
53
			}
54
55 1
			if ($link) {
56
				if (
57 1
					strpos($link, '#error') === 0 ||
58 1
					(strrpos($href, "!") !== false && strpos($link, '#') === 0)
59
				) {
60
					continue; // Did not find signal handler
61
				}
62
63 1
				return $link; // Found signal handler!
64
			} else {
65
				continue; // Did not find signal handler
66
			}
67
68
			if ($targetComponent instanceof Presenter) {
0 ignored issues
show
Unused Code introduced by
if ($targetComponent ins...rid, $href, $params); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The class Nette\Application\UI\Presenter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
69
				// Went the whole way up to the UI\Presenter and did not find any signal handler
70
				$this->throwHierarchyLookupException($grid, $href, $params);
71
			}
72
		}
73
74
		// Went 10 steps up to the UI\Presenter and did not find any signal handler
75
		$this->throwHierarchyLookupException($grid, $href, $params);
76
	}
77
78
79
	/**
80
	 * @throws DataGridLinkCreationException
81
	 */
82
	private function throwHierarchyLookupException(DataGrid $grid, $href, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
	{
84
		$desiredHandler = get_class($grid->getParent()) . '::handle' . ucfirst($href) . '()';
85
86
		throw new DataGridLinkCreationException(
87
			'DataGrid could not create link "'
88
			. $href . '" - did not find any signal handler in componenet hierarchy from '
89
			. get_class($grid->getParent()) . ' up to the '
90
			. get_class($grid->getPresenter()) . '. '
91
			. 'Try adding handler ' . $desiredHandler
92
		);
93
	}
94
}
95