SoftManagedModelTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 15
loc 30
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getReferenceSoft() 15 15 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
namespace LRC\Repository;
4
5
/**
6
 * Soft-deletion-aware foreign-key reference resolution for repository models using repository manager.
7
 */
8
trait SoftManagedModelTrait
9
{
10
    use ManagedModelTrait;
11
    
12
    
13
    /**
14
     * Retrieve a reference by name, ignoring soft-deleted entries.
15
     *
16
     * @param string    $name       Reference name.
17
     *
18
     * @return mixed                Model instance if found, null otherwise.
19
     *
20
     * @throws RepositoryException  If this model or the referenced model is not handled by a managed repository, or the referenced repository is not soft-deletion aware.
21
     */
22 3 View Code Duplication
    public function getReferenceSoft($name)
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...
23
    {
24 3
        if (!isset($this->_manager)) {
25 1
            throw new RepositoryException('Model is not handled by a managed repository');
26
        }
27 2
        if (array_key_exists($name, $this->_references)) {
28 2
            $ref = $this->_references[$name];
29 2
            $repo = $this->_manager->getByClass($ref['model']);
30 2
            if (!$repo || !($repo instanceof SoftRepositoryInterface)) {
31 1
                throw new RepositoryException('Referenced model is not handled by a managed soft-deletion-aware repository');
32
            }
33 1
            return ($repo->findSoft($ref['key'], $this->{$ref['attribute']}) ?: null);
34
        }
35 1
        return null;
36
    }
37
}
38