1 | <?php |
||
8 | class PlanParser extends AbstractCpParser |
||
9 | { |
||
10 | /** |
||
11 | * @param string $url |
||
12 | * |
||
13 | * @return string |
||
14 | * @throws \Exception |
||
15 | */ |
||
16 | 2 | public function parseToJson($url) |
|
17 | { |
||
18 | 2 | $this->loadContent($url); |
|
19 | 2 | $weeks = $this->parser->find('#plans table'); |
|
20 | 2 | if (0 >= $weeks->count()) { |
|
21 | 1 | throw new \Exception(sprintf('Plan not found for this url: %s', $url)); |
|
22 | } |
||
23 | |||
24 | 1 | $plan = $this->getPlan(); |
|
25 | 1 | foreach ($weeks as $training) { |
|
|
|||
26 | 1 | $week = ['name' => $training->find('thead tr')->find('td')[1]->innerHtml]; |
|
27 | 1 | foreach ($training->find('tbody tr') as $seance) { |
|
28 | $training = [ |
||
29 | 1 | 'type' => strip_tags($seance->find('td')[0]->innerHtml), |
|
30 | 1 | 'content' => strip_tags($seance->find('td')[1]->innerHtml), |
|
31 | 1 | ]; |
|
32 | 1 | $week['trainings'][] = $training; |
|
33 | 1 | } |
|
34 | 1 | $plan['weeks'][] = $week; |
|
35 | 1 | } |
|
36 | |||
37 | 1 | return json_encode($plan, true); |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $url |
||
42 | * |
||
43 | * @return string |
||
44 | * @throws \Exception |
||
45 | */ |
||
46 | 1 | public function parseToHtml($url) |
|
52 | |||
53 | /** |
||
54 | * @return array |
||
55 | */ |
||
56 | 1 | private function getPlan() |
|
67 | } |
||
68 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.