Completed
Pull Request — master (#34)
by
unknown
01:45
created

Urlset   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 88
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addUrl() 0 6 1
B generateXML() 0 24 4
A getUrls() 0 4 1
A appendSubelementAttribute() 0 11 2
1
<?php
2
3
namespace Thepixeldeveloper\Sitemap;
4
5
use XMLWriter;
6
7
/**
8
 * Class Urlset
9
 *
10
 * @package Thepixeldeveloper\Sitemap
11
 */
12
class Urlset implements OutputInterface
13
{
14
    /**
15
     * Array of URL objects.
16
     *
17
     * @var OutputInterface[]
18
     */
19
    protected $urls = [];
20
21
    /**
22
     * Sub-elements that have been appended to the collection attributes.
23
     *
24
     * @var AppendAttributeInterface[]
25
     */
26
    protected $appendedSubelements = [];
27
28
    /**
29
     * Add a new URL object.
30
     *
31
     * @param OutputInterface $url
32
     *
33
     * @return $this
34
     */
35
    public function addUrl(OutputInterface $url)
36
    {
37
        $this->urls[] = $url;
38
39
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function generateXML(XMLWriter $XMLWriter)
46
    {
47
        $XMLWriter->startElement('urlset');
48
49
        $XMLWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
50
51
        $XMLWriter->writeAttribute('xsi:schemaLocation',
52
            'http://www.sitemaps.org/schemas/sitemap/0.9 ' .
53
            'http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd');
54
55
        $XMLWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
56
57
        foreach ($this->getUrls() as $url) {
58
            foreach ($url->getSubelementsThatAppend() as $subelement) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Thepixeldeveloper\Sitemap\OutputInterface as the method getSubelementsThatAppend() does only exist in the following implementations of said interface: Thepixeldeveloper\Sitemap\Url.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
59
                $this->appendSubelementAttribute($XMLWriter, $subelement);
60
            }
61
        }
62
63
        foreach ($this->getUrls() as $url) {
64
            $url->generateXML($XMLWriter);
65
        }
66
67
        $XMLWriter->endElement();
68
    }
69
70
    /**
71
     * Get array of URL objects.
72
     * 
73
     * @return OutputInterface[]
74
     */
75
    public function getUrls()
76
    {
77
        return $this->urls;
78
    }
79
80
    /**
81
     * Appends the sub-element to the collection attributes if it has yet to be visited.
82
     *
83
     * @param XMLWriter $XMLWriter
84
     * @param OutputInterface $subelement
85
     *
86
     * @return boolean
87
     */
88
    public function appendSubelementAttribute(XMLWriter $XMLWriter, OutputInterface $subelement)
89
    {
90
        if (array_key_exists(get_class($subelement), $this->appendedSubelements)) {
91
            return false;
92
        }
93
94
        $subelement->appendAttributeToCollectionXML($XMLWriter);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Thepixeldeveloper\Sitemap\OutputInterface as the method appendAttributeToCollectionXML() does only exist in the following implementations of said interface: Thepixeldeveloper\Sitemap\Subelements\Image, Thepixeldeveloper\Sitemap\Subelements\Link, Thepixeldeveloper\Sitemap\Subelements\Mobile, Thepixeldeveloper\Sitemap\Subelements\News, Thepixeldeveloper\Sitemap\Subelements\Video.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
95
        $this->appendedSubelements[get_class($subelement)] = $subelement;
96
97
        return true;
98
    }
99
}
100