Completed
Push — develop ( fbdd82...317691 )
by Mike
09:29
created

Renderer/Template/Action/SourcecodeHandler.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.4
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Application\Renderer\Template\Action;
13
14
use phpDocumentor\DomainModel\Renderer\Template\Action;
15
16
/**
17
 * Sourcecode transformation writer; generates syntax highlighted source files in a destination's subfolder.
18
 */
19
class SourcecodeHandler
20
{
21
    private $analyzer;
22
23
    /**
24
     * Sourcecode constructor.
25
     */
26
    public function __construct(Analyzer $analyzer)
27
    {
28
        $this->analyzer = $analyzer;
29
    }
30
31
    /**
32
     * This method writes every source code entry in the structure file to a highlighted file.
33
     *
34
     * @param Sourcecode $action
35
     *
36
     * @return void
37
     */
38
    public function __invoke(Action $action)
39
    {
40
        $artifact = $action->getRenderContext()->getDestination() . '/' . ltrim($action->getDestination(), '\\/');
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface phpDocumentor\DomainModel\Renderer\Template\Action as the method getRenderContext() does only exist in the following implementations of said interface: phpDocumentor\Applicatio...plate\Action\AppendFile, phpDocumentor\Applicatio...plate\Action\Checkstyle, phpDocumentor\Applicatio...emplate\Action\CopyFile, phpDocumentor\Applicatio...r\Template\Action\Graph, phpDocumentor\Applicatio...r\Template\Action\Jsonp, phpDocumentor\Applicatio...plate\Action\Sourcecode, phpDocumentor\Applicatio...er\Template\Action\Twig, phpDocumentor\Applicatio...rer\Template\Action\Xml, phpDocumentor\Applicatio...rer\Template\Action\Xsl.

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...
41
        $project = $this->analyzer->getProjectDescriptor();
42
43
        /** @var FileDescriptor $file */
44
        foreach ($project->getFiles() as $file) {
45
            $filename = $file->getPath();
46
            $source   = $file->getSource();
47
48
            $root = str_repeat('../', count(explode(DIRECTORY_SEPARATOR, $filename)));
49
            $path = $artifact . DIRECTORY_SEPARATOR . $filename;
50
            if (!file_exists(dirname($path))) {
51
                mkdir(dirname($path), 0755, true);
52
            }
53
            $source = htmlentities($source);
54
55
            $fs = $action->getRenderContext()->getFilesystem();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface phpDocumentor\DomainModel\Renderer\Template\Action as the method getRenderContext() does only exist in the following implementations of said interface: phpDocumentor\Applicatio...plate\Action\AppendFile, phpDocumentor\Applicatio...plate\Action\Checkstyle, phpDocumentor\Applicatio...emplate\Action\CopyFile, phpDocumentor\Applicatio...r\Template\Action\Graph, phpDocumentor\Applicatio...r\Template\Action\Jsonp, phpDocumentor\Applicatio...plate\Action\Sourcecode, phpDocumentor\Applicatio...er\Template\Action\Twig, phpDocumentor\Applicatio...rer\Template\Action\Xml, phpDocumentor\Applicatio...rer\Template\Action\Xsl.

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...
56
            $fs->put(
57
                $path.'.html',
58
                <<<HTML
59
<html>
60
    <head>
61
        <script
62
            type="text/javascript"
63
            src="{$root}js/jquery-1.4.2.min.js">
64
        </script>
65
        <script
66
            type="text/javascript"
67
            src="{$root}syntax_highlighter/scripts/shCore.js">
68
        </script>
69
        <script
70
            type="text/javascript"
71
            src="{$root}syntax_highlighter/scripts/shBrushJScript.js">
72
        </script>
73
        <script
74
            type="text/javascript"
75
            src="{$root}syntax_highlighter/scripts/shBrushPhp.js">
76
        </script>
77
        <script
78
            type="text/javascript"
79
            src="{$root}syntax_highlighter/scripts/shBrushXml.js">
80
        </script>
81
        <link
82
            href="{$root}syntax_highlighter/styles/shCore.css" rel="stylesheet"
83
            type="text/css"
84
        />
85
        <link
86
            href="{$root}syntax_highlighter/styles/shCoreEclipse.css"
87
            rel="stylesheet" type="text/css"
88
        />
89
        <link
90
            href="{$root}syntax_highlighter/styles/shThemeWordpress.css"
91
            rel="stylesheet" type="text/css"
92
        />
93
    </head>
94
    <body>
95
        <pre class="brush: php">$source</pre>
96
        <script type="text/javascript">
97
             SyntaxHighlighter.all();
98
             jQuery('.gutter div').each(function(key, data){
99
                jQuery(data).prepend('<a name="L'+jQuery(data).text()+'"/>');
100
             });
101
        </script>
102
    </body>
103
</html>
104
HTML
105
            );
106
107
        }
108
    }
109
}
110