Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

ArticleProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 10.26 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 8
loc 78
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getBaseNode() 0 4 1
A getOneById() 8 8 2
A getParent() 0 4 1
A getTenantArticlesQuery() 0 4 1
A getRouteArticlesQuery() 0 4 1

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Provider\ODM\PHPCR;
18
19
use Jackalope\Query\SqlQuery;
20
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
21
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface;
22
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface;
23
24
/**
25
 * ArticleProvider to provide articles based on PHPCR paths.
26
 */
27
class ArticleProvider implements ArticleProviderInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: getCountByCriteria, getManyByCriteria, getOneByCriteria, getRepository
Loading history...
28
{
29
    /**
30
     * @var ArticleRepositoryInterface
31
     */
32
    private $articleRepository;
33
34
    /**
35
     * @var TenantAwarePathBuilderInterface
36
     */
37
    private $pathBuilder;
38
39
    /**
40
     * @var string
41
     */
42
    private $basePath;
43
44
    /**
45
     * ArticleProvider constructor.
46
     *
47
     * @param ArticleRepositoryInterface      $articleRepository
48
     * @param TenantAwarePathBuilderInterface $pathBuilder
49
     * @param string                          $basePath
50
     */
51
    public function __construct(
52
        ArticleRepositoryInterface $articleRepository,
53
        TenantAwarePathBuilderInterface $pathBuilder,
54
        string $basePath
55
    ) {
56
        $this->articleRepository = $articleRepository;
57
        $this->pathBuilder = $pathBuilder;
58
        $this->basePath = $basePath;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getBaseNode()
65
    {
66
        return $this->articleRepository->findBaseNode($this->pathBuilder->build($this->basePath));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface SWP\Bundle\ContentBundle...icleRepositoryInterface as the method findBaseNode() does only exist in the following implementations of said interface: SWP\Bundle\ContentBundle...PHPCR\ArticleRepository.

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...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 View Code Duplication
    public function getOneById($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
73
    {
74
        if (!filter_var($id, FILTER_VALIDATE_INT)) {
75
            return $this->articleRepository->findOneBySlug($id);
76
        }
77
78
        return $this->articleRepository->findOneBy(['id' => $id]);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getParent($id)
85
    {
86
        return $this->articleRepository->find($this->pathBuilder->build($id));
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getTenantArticlesQuery(string $tenantContentIdentifier, array $order) : SqlQuery
93
    {
94
        return $this->articleRepository->getQueryForTenantArticles($tenantContentIdentifier, $order);
0 ignored issues
show
Bug introduced by
The method getQueryForTenantArticles() does not seem to exist on object<SWP\Bundle\Conten...cleRepositoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getRouteArticlesQuery(string $routeIdentifier, array $order) : SqlQuery
101
    {
102
        return $this->articleRepository->getQueryForRouteArticles($routeIdentifier, $order);
103
    }
104
}
105