Completed
Pull Request — master (#8)
by Andreas
01:57
created

PhpNetCfpParser::parse()   C

Complexity

Conditions 8
Paths 85

Size

Total Lines 83
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 83
rs 5.8854
cc 8
eloc 52
nc 85
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Zend Framework (http://framework.zend.com/)
4
 *
5
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7
 * @license   http://framework.zend.com/license/new-bsd New BSD License
8
 */
9
10
namespace Callingallpapers\Parser;
11
12
use Callingallpapers\Entity\Cfp;
13
use Callingallpapers\Writer\WriterInterface;
14
use GuzzleHttp\Client;
15
16
class PhpNetCfpParser implements ParserInterface
17
{
18
19
    public function parse(WriterInterface $writer)
20
    {
21
        $uri = 'http://php.net/archive/archive.xml';
22
        $client = new Client();
23
        $content = $client->get($uri)->getBody();
24
25
        $contents = new \ArrayObject();
0 ignored issues
show
Unused Code introduced by
$contents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        $now = new \DateTimeImmutable();
27
        $then = $now->sub(new \DateInterval('P1Y'));
28
29
        $dom = new \DOMDocument('1.0', 'UTF-8');
30
        $dom->loadXML($content, LIBXML_NOBLANKS ^ LIBXML_XINCLUDE);
31
        $dom->documentURI = $uri;
32
33
        $xpath = new \DOMXPath($dom);
34
        $nodes = $xpath->query('//xi:include[@href]', $dom->parentNode);
35
36
        foreach ($nodes as $item) {
37
            /** @var \DOMNode $item */
38
            $href = $item->attributes->getNamedItem('href');
39
            if (! preg_match('/\/([\d\-]{10})/', $href->textContent, $result)) {
40
                continue;
41
            }
42
43
            $date = new \DateTime($result[1]);
44
45
            if (! $date instanceof \DateTime) {
46
                continue;
47
            }
48
49
            if ($then > $date) {
50
                $item->parentNode->removeChild($item);
51
                continue;
52
            }
53
        }
54
55
        $dom->xinclude();
56
        $dom->normalizeDocument();
57
58
        $xpath->registerNamespace('default', 'http://php.net/ns/news');
59
        $xpath->registerNamespace('f', 'http://www.w3.org/2005/Atom');
60
61
        $items = $xpath->query('//f:category[@term="cfp"]');
62
63
        foreach ($items as $node) {
64
            try {
65
                /** @var \DOMNode $node */
66
                $node    = $node->parentNode;
67
                $item    = $xpath->query('default:finalTeaserDate', $node)->item(0);
68
                $cfpDate = new \DateTime($item->textContent);
69
70
                if ($now > $cfpDate) {
71
                    continue;
72
                }
73
74
                $item = $xpath->query('published', $node)->item(0);
75
                $cfpStart = new \DateTime($item->textContent);
76
                var_Dump($cfpStart);
0 ignored issues
show
Security Debugging Code introduced by
var_Dump($cfpStart); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
77
78
                $info = new Cfp();
79
80
                $nameNodes            = $xpath->query('f:title', $node);
81
                $info->conferenceName = $nameNodes->item(0)->textContent;
82
83
                $descNode          = $xpath->query('f:content', $node)->item(0);
84
                $info->description = $dom->saveXML($descNode);
85
86
                $info->dateEnd   = $cfpDate;
87
                $info->dateStart = $cfpStart;
88
                $info->tags      = ['PHP'];
89
90
                $cfpImageNode    = $xpath->query('default:newsImage', $node)->item(0);
91
                $info->uri       = $cfpImageNode->attributes->getNamedItem('link')->textContent;
92
                $info->conferenceUri = $cfpImageNode->attributes->getNamedItem('link')->textContent;
93
                $info->iconUri = 'http://php.net/images/news/' . $cfpImageNode->textContent;
94
95
                var_Dump($info->toArray());
96
          //      $writer->write($info, 'php.net');
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
            } catch (\Exception $e) {
98
                echo $e->getMessage() . "\n";
99
            }
100
        }
101
    }
102
}
103