1 | <?php |
||
7 | class PreviewBuilder |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * PHPQuery document object that will be used to select elements |
||
12 | * @var \Jclyons52\PHPQuery\Document |
||
13 | */ |
||
14 | public $document; |
||
15 | |||
16 | /** |
||
17 | * reference to the url parameter the user passes into the fetch method |
||
18 | * @var string |
||
19 | */ |
||
20 | public $url; |
||
21 | |||
22 | /** |
||
23 | * destructured array of url components from parse_url |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $urlComponents; |
||
27 | |||
28 | /** |
||
29 | * @var HttpInterface |
||
30 | */ |
||
31 | protected $http; |
||
32 | |||
33 | 81 | public function __construct(HttpInterface $http) |
|
34 | { |
||
35 | 81 | $this->http = $http; |
|
36 | 81 | } |
|
37 | |||
38 | /** |
||
39 | * Instantiate class with dependencies |
||
40 | * @return static |
||
41 | */ |
||
42 | 25 | public static function create() |
|
43 | { |
||
44 | 3 | $http = new Http(); |
|
45 | 3 | return new PreviewBuilder($http); |
|
46 | 24 | } |
|
47 | |||
48 | /** |
||
49 | * @param string $url |
||
50 | * @return Preview |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | 45 | public function fetch($url) |
|
54 | { |
||
55 | 45 | $this->url = $url; |
|
56 | |||
57 | 45 | $urlComponents = parse_url($url); |
|
58 | |||
59 | 45 | if (filter_var($url, FILTER_VALIDATE_URL) === false) { |
|
60 | 3 | throw new \Exception("url {$this->url} is invalid"); |
|
61 | } |
||
62 | 42 | $this->urlComponents = $urlComponents; |
|
|
|||
63 | |||
64 | 42 | $body = $this->http->get($url); |
|
65 | |||
66 | 42 | if ($body === false) { |
|
67 | 3 | throw new \Exception('failed to load page'); |
|
68 | } |
||
69 | |||
70 | 39 | $this->document = new Document($body); |
|
71 | |||
72 | 39 | return $this->getPreview(); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | 39 | public function title() |
|
82 | |||
83 | /** |
||
84 | * @return mixed |
||
85 | */ |
||
86 | 39 | public function metaKeywords() |
|
103 | |||
104 | /** |
||
105 | * @param string $element |
||
106 | * @return array |
||
107 | */ |
||
108 | 39 | public function meta($element = null) |
|
123 | |||
124 | /** |
||
125 | * get source attributes of all image tags on the page |
||
126 | * @return array<String> |
||
127 | */ |
||
128 | 39 | public function images() |
|
142 | |||
143 | /** |
||
144 | * returns an instance of Preview |
||
145 | * @return Preview |
||
146 | */ |
||
147 | 39 | public function getPreview() |
|
173 | |||
174 | /** |
||
175 | * @param string $url |
||
176 | * @return string |
||
177 | */ |
||
178 | 36 | private function formatUrl($url) |
|
198 | |||
199 | /** |
||
200 | * @param \Jclyons52\PHPQuery\Support\NodeCollection $metaTags |
||
201 | * @return array |
||
202 | */ |
||
203 | 39 | private function metaTagsToArray($metaTags) |
|
220 | } |
||
221 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.