Completed
Pull Request — master (#28)
by Andreas
05:00
created

PapercallIoParser::parse()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 6
eloc 23
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright (c) Andreas Heigl<[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 *
24
 * @author    Andreas Heigl<[email protected]>
25
 * @copyright Andreas Heigl
26
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
27
 * @since     29.07.2016
28
 * @link      http://github.com/heiglandreas/callingallpapers
29
 */
30
31
namespace Callingallpapers\Parser\PapercallIo;
32
33
use Callingallpapers\Parser\ParserInterface;
34
use Callingallpapers\Writer\WriterInterface;
35
use IvoPetkov\HTML5DOMDocument as DOMDocument;
36
use DOMXPath;
37
38
class PapercallIoParser implements ParserInterface
39
{
40
    const SOURCE = 'papercallio';
41
42
    private $uri = 'https://papercall.io/cfps?page=%1$s';
43
44
    private $parser;
45
46
    public function __construct(EventParser $parser)
47
    {
48
        $this->parser = $parser;
49
    }
50
51
    public function setStartUrl(string $uri)
52
    {
53
        $this->uri = $uri;
54
    }
55
56
    public function parse(WriterInterface $writer)
57
    {
58
        $i = 0;
59
60
        $pages = null;
61
62
        do {
63
64
            $dom = new DOMDocument('1.0', 'UTF-8');
65
            libxml_use_internal_errors(true);
66
            $uri = sprintf($this->uri, $i+1);
67
            $dom->loadHTMLFile($uri);
68
            libxml_use_internal_errors(false);
69
            $dom->preserveWhiteSpace = false;
70
            $xpath = new DOMXPath($dom);
71
72
            if (null === $pages) {
73
                $p = $xpath->query("//ul[contains(@class,'pagination')]/li");
74
                $pages = $p->length - 2;
75
            }
76
77
            $nodes = $xpath->query("//div[contains(@class,'main')]/div[@class='container'][last()]//div[@class='box']");
78
            if ($nodes->length < 1) {
79
                continue;
80
            }
81
82
            /** @var DOMNode $node */
83
            foreach ($nodes as $node) {
84
                try {
85
                    $writer->write($this->parser->parseEvent($node), self::SOURCE);
86
                } catch (\Exception $e) {
87
                    // Do nothing
88
                }
89
            }
90
        } while (++$i < $pages);
91
92
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type declared by the interface Callingallpapers\Parser\ParserInterface::parse of type Callingallpapers\Entity\CfpList.

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...
93
    }
94
}
95