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

Renderer/Template/Action/AppendFileHandler.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 League\Flysystem\Adapter\Local;
15
use phpDocumentor\DomainModel\Renderer\Template\Action;
16
use phpDocumentor\DomainModel\Renderer\Template\ActionHandler;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Webmozart\Assert\Assert;
19
20
class AppendFileHandler implements ActionHandler
21
{
22
    /**
23
     * @var Filesystem
24
     */
25
    private $filesystem;
26
27
    public function __construct(Filesystem $filesystem)
28
    {
29
        $this->filesystem = $filesystem;
30
    }
31
32
    /**
33
     * Executes the activities that this Action represents.
34
     *
35
     * @param AppendFile $action
36
     *
37
     * @todo this method still acts as a Copy because I currently do not know how to append with stream,
38
     *       fix this as soon as I got internet.
39
     *
40
     * @return void
41
     */
42
    public function __invoke(Action $action)
43
    {
44
        $source      = $this->getSourceLocation($action);
45
        $destination = $this->getDestination($action);
46
47
        Assert::fileExists($source);
48
        Assert::writable(dirname($destination));
49
50
        $destinationFS = $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...
51
52
        if (is_file($source)) {
53
            $stream = fopen($source, 'r+');
54
            $destinationFS->putStream($destination, $stream);
55
            fclose($stream);
56
        } else {
57
            $sourceFS = new \League\Flysystem\Filesystem(new Local($source));
58
            foreach ($sourceFS->listContents('', true) as $path) {
59
                $path = $path['path'];
60
61
                if ($sourceFS->getMetadata($path)['type'] === 'dir') {
62
                    continue;
63
                }
64
65
                $destinationFS->putStream($destination . '/' . $path, $sourceFS->readStream($path));
66
            }
67
        }
68
    }
69
70
    /**
71
     * @param AppendFile $action
72
     *
73
     * @return string
74
     */
75
    private function getSourceLocation(Action $action)
76
    {
77
        $source = (string)$action->getSource();
78
        if (! $this->filesystem->isAbsolutePath($source)) {
79
            if (file_exists(getcwd() . '/' . $source)) {
80
                return getcwd() . '/' . $source;
81
            }
82
            if (file_exists(__DIR__ . '/../../../../' . $source)) {
83
                return __DIR__ . '/../../../../' . $source;
84
            }
85
86
            return __DIR__ . '/../../../../data/' . $source;
87
        }
88
89
        return $source;
90
    }
91
92
    /**
93
     * @param CopyFile $action
94
     *
95
     * @return string
96
     */
97
    private function getDestination(Action $action)
98
    {
99
        $destination = $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...
100
101
        return $destination;
102
    }
103
}
104