Completed
Pull Request — master (#34)
by
unknown
03:04
created

Urlset::isSubelementAppended()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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
                if (!$this->isSubelementAppended($subelement)) {
60
                    $subelement->appendAttributeToCollectionXML($XMLWriter);
61
                    $this->appendedSubelements[get_class($subelement)] = $subelement;
62
                }
63
            }
64
        }
65
66
        foreach ($this->getUrls() as $url) {
67
            $url->generateXML($XMLWriter);
68
        }
69
70
        $XMLWriter->endElement();
71
    }
72
73
    /**
74
     * Get array of URL objects.
75
     * 
76
     * @return OutputInterface[]
77
     */
78
    public function getUrls()
79
    {
80
        return $this->urls;
81
    }
82
83
    /**
84
     * Checks if the sub-element has been appended to the collection attributes.
85
     *
86
     * @param OutputInterface $subelement
87
     *
88
     * @return boolean
89
     */
90
    protected function isSubelementAppended(OutputInterface $subelement)
91
    {
92
        return array_key_exists(get_class($subelement), $this->appendedSubelements);
93
    }
94
}
95