Completed
Push — master ( ad16ce...5852b3 )
by Jacob
07:53
created

PieceController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 85
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A set_data() 0 15 1
A get_piece_data() 0 55 4
1
<?
0 ignored issues
show
Security Best Practice introduced by
It is not recommend to use PHP's short opening tag <?, better use <?php, or <?= in case of outputting.

Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.

As a precaution to avoid these problems better use the long opening tag <?php.

Loading history...
2
3
Loader::load('controller', 'portfolio/DefaultPageController');
4
5
final class PieceController extends DefaultPageController
6
{
7
8
	private $title_url;
9
10
	function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
11
	{
12
		parent::__construct();
13
		
14
		$this->title_url = URLDecode::getPiece(2);
15
	}
16
17
	protected function set_data()
18
	{
19
		$this->set_title("Jacob Emerick's Portfolio");
20
		$this->set_head('description', "Jacob Emerick's Portfolio - examples of my work ranging from early print design to current web projects");
21
		$this->set_head('keywords', 'portfolio, Jacob Emerick, print design, examples, advertising, marketing campaigns, freelance, graphic design');
22
		
23
		$this->set_body('body_view', 'Piece');
24
		$this->set_body('left_side_data', array(
25
			'title' => "Print Gallery | Jacob Emerick's Portfolio",
26
			'menu' => $this->get_menu(),
0 ignored issues
show
Bug introduced by
The method get_menu() does not seem to exist on object<PieceController>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
			'home_link' => Loader::getRootURL()));
28
		$this->set_body('body_data', $this->get_piece_data());
29
		
30
		$this->set_body_view('Page');
31
	}
32
33
	private function get_piece_data()
34
	{
35
		Loader::load('collector', 'portfolio/PortfolioCollector');
36
		$portfolio_result = PortfolioCollector::getPieceByURI($this->title_url);
37
		
38
		if($portfolio_result === null)
39
			$this->eject();
40
		
41
		$portfolio_image_result = PortfolioCollector::getImagesForPiece($portfolio_result->id, 2);
42
43
    $image_path = "portfolio/{$portfolio_image_result[0]->name}";
44
    $image_path = Loader::getImagePath('image', $image_path);
45
    $image_size = getimagesize($image_path);
46
47
		$main_image = new stdclass();
48
		$main_image->id = $portfolio_image_result[0]->id;
49
		$main_image->link = "/image/portfolio/{$portfolio_image_result[0]->name}";
50
		
51
		$main_image->width = $image_size[0];
52
		$main_image->height = $image_size[1];
53
		
54
		foreach($portfolio_image_result as $portfolio_image)
0 ignored issues
show
Bug introduced by
The expression $portfolio_image_result of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
55
		{
56
			$thumb = $portfolio_image->name;
57
			$thumb_array = explode('.', $thumb);
58
			$thumb = "{$thumb_array[0]}_clip.{$thumb_array[1]}";
59
60
      $image_path = "portfolio/{$thumb}";
61
      $image_path = Loader::getImagePath('image', $image_path);
62
      $image_size = getimagesize($image_path);
63
64
			$image_obj = new stdclass();
65
			$image_obj->id = $portfolio_image->id;
66
			$image_obj->link = "/image/portfolio/{$thumb}";
67
			
68
			$image_obj->width = $image_size[0];
69
			$image_obj->height = $image_size[1];
70
			
71
			$image_array[] = $image_obj;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$image_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $image_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
72
		}
73
		
74
		$portfolio_tag_result = PortfolioCollector::getTagsForPiece($portfolio_result->id);
75
		
76
		foreach($portfolio_tag_result as $portfolio_tag)
0 ignored issues
show
Bug introduced by
The expression $portfolio_tag_result of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
77
		{
78
			$tag_array[] = $portfolio_tag->name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tag_array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tag_array = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
79
		}
80
		
81
		return array(
82
			'title' => $portfolio_result->title,
83
			'description' => $portfolio_result->description,
84
			'thumbs' => $image_array,
0 ignored issues
show
Bug introduced by
The variable $image_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
85
			'image' => $main_image,
86
			'tags' => $tag_array);
0 ignored issues
show
Bug introduced by
The variable $tag_array does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
87
	}
88
89
}
90