Completed
Push — master ( f459ca...44ae62 )
by Cyril
02:53
created

ObjectToPreview::set()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 10

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 24
ccs 0
cts 24
cp 0
rs 8.5125
cc 6
eloc 10
nc 6
nop 5
crap 42
1
<?php
2
/**
3
 *
4
 */
5
namespace ApaiIO\ResponseTransformer;
6
7
/**
8
 *
9
 */
10
class ObjectToPreview extends ObjectToArray implements ResponseTransformerInterface
11
{
12
	/**
13
	 *
14
	 * @var type
15
	 */
16
    protected $data = array();
17
18
	/**
19
	 *
20
	 * @var type
21
	 */
22
	protected $items = array();
23
24
	/**
25
	 *
26
	 * @param type $response
27
	 * @return type
28
	 */
29
	public function transform($response)
30
    {
31
        $response = parent::transform($response);
32
33
        if( ! $this->get_items( $response ) )
34
		{
35
			return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by ApaiIO\ResponseTransform...ectToPreview::transform of type ApaiIO\ResponseTransformer\type.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
		}
37
        $c = count( $this->items );
38
        for( $i=0; $i < $c; $i++ )
39
        {
40
            $this->set( $i, 'asin', 'ASIN' );
0 ignored issues
show
Documentation introduced by
'asin' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'ASIN' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
            $this->set( $i, 'sales_rank', 'SalesRank' );
0 ignored issues
show
Documentation introduced by
'sales_rank' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'SalesRank' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
            $this->set( $i, 'title', 'ItemAttributes', 'Title' );
0 ignored issues
show
Documentation introduced by
'title' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'ItemAttributes' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'Title' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
            $this->set( $i, 'url', 'DetailPageURL');
0 ignored issues
show
Documentation introduced by
'url' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'DetailPageURL' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
            $this->set( $i, 'manufacturer', 'ItemAttributes', 'Manufacturer' );
0 ignored issues
show
Documentation introduced by
'manufacturer' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'ItemAttributes' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'Manufacturer' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
            $this->set( $i, 'large_image', 'LargeImage', 'URL' );
0 ignored issues
show
Documentation introduced by
'large_image' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'LargeImage' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'URL' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
            $this->set( $i, 'medium_image', 'MediumImage', 'URL' );
0 ignored issues
show
Documentation introduced by
'medium_image' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'MediumImage' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'URL' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
            $this->set( $i, 'small_image', 'SmallImage', 'URL' );
0 ignored issues
show
Documentation introduced by
'small_image' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'SmallImage' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'URL' is of type string, but the function expects a object<ApaiIO\ResponseTransformer\type>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
            $this->get_price($i);
49
        }
50
51
        return $this->data;
52
    }
53
54
	/**
55
	 *
56
	 * @param type $response
57
	 * @return mixed
58
	 */
59 View Code Duplication
	protected function get_items($response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
	{
61
		if( isset($response['Items']['Item']) AND is_array($response['Items']['Item']) )
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
62
		{
63
			return $this->items = $response['Items']['Item'];
64
		}
65
		else
66
		{
67
			return FALSE;
68
		}
69
	}
70
71
72
	/**
73
	 *
74
	 * @param type $data
75
	 * @param type $key1
76
	 * @param type $key2
77
	 * @param type $key3
78
	 */
79 View Code Duplication
	protected function set($i, $data, $key1, $key2=NULL, $key3=NULL)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
	{
81
		if($key3)
82
		{
83
			if( isset($this->items[$i][$key1][$key2][$key3]) )
84
			{
85
				$this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3];
86
			}
87
		}
88
		elseif($key2)
89
		{
90
			if( isset($this->items[$i][$key1][$key2]) )
91
			{
92
				$this->data[$i][$data] = $this->items[$i][$key1][$key2];
93
			}
94
		}
95
		else
96
		{
97
			if( isset($this->items[$i][$key1]) )
98
			{
99
				$this->data[$i][$data] = $this->items[$i][$key1];
100
			}
101
		}
102
	}
103
104 View Code Duplication
    protected function set_array($i, $data, $key1, $key2=NULL, $key3=NULL)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $this->set($i, $data, $key1, $key2, $key3);
107
        if(isset($this->data[$i][$data]) AND !is_array( $this->data[$i][$data]))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
108
        {
109
            $this->data[$i][$data] = array($this->data[$i][$data]);
110
        }
111
    }
112
113 View Code Duplication
    private function get_price($i)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $list_price = isset( $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] ) ? $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] : NULL;
116
        $amazon_price = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] : NULL;
117
        $saved = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved']['Amount'] : NULL;
118
        $price = ($list_price) ? $list_price : ($amazon_price ? ($amazon_price + $saved) : NULL );
119
        $this->data[$i]['price'] = ($price) ? $price : (isset($this->data[$i]['lowest_new_price']) ? $this->data[$i]['lowest_new_price'] : NULL);
120
    }
121
}