Completed
Pull Request — master (#59)
by Greg
03:14
created

HelpDocument::generateBaseHelpDom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Consolidation\AnnotatedCommand\Help;
3
4
use Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface;
5
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Descriptor\XmlDescriptor;
8
9
class HelpDocument implements DomDataInterface
10
{
11
    /** var Command */
12
    protected $command;
13
14
    /** var \DOMDocument */
15
    protected $dom;
16
17
    /**
18
     * Create a help document from a Symfony Console command
19
     */
20
    public function __construct(Command $command)
21
    {
22
        $dom = static::generateBaseHelpDom($command);
0 ignored issues
show
Bug introduced by
Since generateBaseHelpDom() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of generateBaseHelpDom() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
23
        $dom = static::alterHelpDocument($command, $dom);
0 ignored issues
show
Bug introduced by
Since alterHelpDocument() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of alterHelpDocument() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
24
25
        $this->command = $command;
26
        $this->dom = $dom;
27
    }
28
29
    /**
30
     * Convert data into a \DomDocument.
31
     *
32
     * @return \DomDocument
33
     */
34
    public function getDomData()
35
    {
36
        return $this->dom;
37
    }
38
39
    /**
40
     * Create the base help DOM prior to alteration by the Command object.
41
     * @param Command $command
42
     * @return \DomDocument
43
     */
44
    private static function generateBaseHelpDom(Command $command)
45
    {
46
        // Use Symfony to generate xml text. If other formats are
47
        // requested, convert from xml to the desired form.
48
        $descriptor = new XmlDescriptor();
49
        return $descriptor->getCommandDocument($command);
50
    }
51
52
    /**
53
     * Alter the DOM document per the command object
54
     * @param Command $command
55
     * @param \DomDocument $dom
56
     * @return \DomDocument
57
     */
58
    private static function alterHelpDocument(Command $command, \DomDocument $dom)
59
    {
60
        if ($command instanceof HelpDocumentAlter) {
61
            $dom = $command->helpDocumentAlter($dom);
62
        }
63
        return $dom;
64
    }
65
}
66