Passed
Branch master (a96d1e)
by Nicolas
02:44
created

PlanParser::loadContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.0185
1
<?php
2
3
namespace Cp\Parser;
4
5
use Cp\Http\HeaderParser;
6
use PHPHtmlParser\Dom;
7
8
/**
9
 * Class PlanParser
10
 */
11
class PlanParser
12
{
13
    /**
14
     * @var Dom
15
     */
16
    private $parser;
17
18
    /**
19
     * CpParser constructor.
20
     *
21
     * @param Dom $parser
22
     */
23 3
    public function __construct(Dom $parser)
24
    {
25 3
        $this->parser = $parser;
26 3
    }
27
28
    /**
29
     * @param string $url
30
     *
31
     * @throws \Exception
32
     */
33 3
    public function loadContent($url)
34
    {
35 3
        $htmlContent = file_get_contents($url);
36 3
        if (false === $htmlContent) {
37
            throw new \Exception(sprintf('Content can not be get from %s', $url));
38
        }
39
40 3
        $this->parser->load($htmlContent);
41 3
    }
42
43
    /**
44
     * @param string $url
45
     *
46
     * @return string
47
     * @throws \Exception
48
     */
49 2
    public function parseToJson($url)
50
    {
51 2
        $this->loadContent($url);
52 2
        $weeks = $this->parser->find('#plans table');
53 2
        if (0 >= $weeks->count()) {
54 1
            throw new \Exception(sprintf('Plan not found for this url: %s', $url));
55
        }
56
57 1
        $plan = $this->getPlan();
58 1
        foreach ($weeks as $training) {
0 ignored issues
show
Bug introduced by
The expression $weeks of type array|object<PHPHtmlParser\Dom\AbstractNode> 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...
59 1
            $week = ['name' => $training->find('thead tr')->find('td')[1]->innerHtml];
60 1
            foreach ($training->find('tbody tr') as $seance) {
61
                $training = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
62 1
                    'type' => strip_tags($seance->find('td')[0]->innerHtml),
63 1
                    'content' => strip_tags($seance->find('td')[1]->innerHtml),
64 1
                ];
65 1
                $week['trainings'][] = $training;
66 1
            }
67 1
            $plan['weeks'][] = $week;
68 1
        }
69
70 1
        return json_encode($plan, true);
71
    }
72
73
    /**
74
     * @param string $url
75
     *
76
     * @return string
77
     * @throws \Exception
78
     */
79 1
    public function parseToHtml($url)
80
    {
81 1
        $this->loadContent($url);
82
83 1
        return sprintf('%s%s%s', '<table>', $this->parser->find('#plans table')->innerHtml, '</table>');
84
    }
85
86
    /**
87
     * @return array
88
     */
89 1
    private function getPlan()
90
    {
91 1
        $nameOfPlan = strip_tags($this->parser->find('.article-content-main h1')->innerHtml);
92 1
        $typeOfPlan = strip_tags($this->parser->find('.article-content-main h3')->innerHtml);
93
94
        return [
95 1
            'name' => strip_tags($nameOfPlan),
96 1
            'type' => strip_tags($typeOfPlan),
97 1
            'weeks' => [],
98 1
        ];
99
    }
100
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
101