Completed
Push — feature-20rc1 ( 008ae2 )
by Rob
16:55
created

FileAttributesApplier   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 42.5 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 12
dl 34
loc 80
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B apply() 18 24 3
B assignFileAttributes() 16 24 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\File\Attributes\Resolver;
13
14
use Liip\ImagineBundle\Exception\File\Attributes\Resolver\InvalidFileAttributesException;
15
use Liip\ImagineBundle\File\Attributes\ContentTypeAttribute;
16
use Liip\ImagineBundle\File\Attributes\ExtensionAttribute;
17
use Liip\ImagineBundle\File\FileBlob;
18
use Liip\ImagineBundle\File\FileBlobInterface;
19
use Liip\ImagineBundle\File\FileInterface;
20
use Liip\ImagineBundle\File\FilePath;
21
use Liip\ImagineBundle\File\FilePathInterface;
22
use Psr\Log\LoggerAwareTrait;
23
use Psr\Log\NullLogger;
24
25
/**
26
 * @author Rob Frawley 2nd <[email protected]>
27
 */
28
final class FileAttributesApplier
29
{
30
    use LoggerAwareTrait;
31
32
    /**
33
     * @var FileAttributesResolver
34
     */
35
    private $resolver;
36
37
    /**
38
     * @param FileAttributesResolver $resolver
39
     */
40
    public function __construct(FileAttributesResolver $resolver)
41
    {
42
        $this->resolver = $resolver;
43
        $this->logger = new NullLogger();
44
    }
45
46
    /**
47
     * @param FileInterface|FilePathInterface $file
48
     *
49
     * @return FileInterface|FilePathInterface
50
     */
51
    public function apply(FileInterface $file): FileInterface
52
    {
53 View Code Duplication
        if (!$file->hasContentType()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $attr = $this->resolver->resolve($file);
55
56
            return $this->assignFileAttributes(
57
                $file,
58
                $attr->getContentType(),
59
                $attr->getExtension()
60
            );
61
        }
62
63 View Code Duplication
        if (!$file->hasExtension()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
            $attr = $this->resolver->resolve($file);
65
66
            return $this->assignFileAttributes(
67
                $file,
68
                $file->getContentType(),
69
                $attr->getExtension()
70
            );
71
        }
72
73
        return $file;
74
    }
75
76
    /**
77
     * @param FileInterface|FileBlobInterface|FilePathInterface $file
78
     * @param ContentTypeAttribute                              $contentType
79
     * @param ExtensionAttribute                                $extension
80
     *
81
     * @return FileInterface|FileBlobInterface|FilePathInterface
82
     */
83
    private function assignFileAttributes(FileInterface $file, ContentTypeAttribute $contentType, ExtensionAttribute $extension): FileInterface
84
    {
85 View Code Duplication
        if (false === $contentType->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
            $this->logger->error($m = sprintf(
87
                'Unable to resolve content type attribute for file %s.',
88
                $file->hasFile() ? $file->getFile()->getPathname() : 'blob'
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Liip\ImagineBundle\File\FileInterface as the method getFile() does only exist in the following implementations of said interface: Liip\ImagineBundle\File\FilePath, Liip\ImagineBundle\File\FileTemp.

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...
89
            ));
90
91
            throw new InvalidFileAttributesException($m);
92
        }
93
94 View Code Duplication
        if (false === $extension->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            $this->logger->error($m = sprintf(
96
                'Unable to resolve extension attribute for file %s.',
97
                $file->hasFile() ? $file->getFile()->getPathname() : 'blob'
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Liip\ImagineBundle\File\FileInterface as the method getFile() does only exist in the following implementations of said interface: Liip\ImagineBundle\File\FilePath, Liip\ImagineBundle\File\FileTemp.

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...
98
            ));
99
100
            throw new InvalidFileAttributesException($m);
101
        }
102
103
        return $file instanceof FilePathInterface
104
            ? new FilePath($file->getFile(), $contentType, $extension)
105
            : new FileBlob($file->getContents(), $contentType, $extension);
106
    }
107
}
108