Completed
Branch develop (f7dc53)
by Anton
05:49
created

FindTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 20.29 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 1
dl 14
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 4 1
A findOne() 0 4 1
A findByPK() 0 4 1
A source() 14 14 2

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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\ODM\Traits;
9
10
use Spiral\ODM\Document;
11
use Spiral\ODM\Entities\DocumentSelector;
12
use Spiral\ODM\Entities\DocumentSource;
13
use Spiral\ODM\ODM;
14
use Spiral\ORM\Exceptions\ORMException;
15
use Spiral\ORM\RecordEntity;
16
17
/**
18
 * Static record functionality including create and find methods.
19
 */
20
trait FindTrait
21
{
22
    /**
23
     * Find multiple records based on provided query.
24
     *
25
     * Example:
26
     * User::find(['status' => 'active'], ['profile']);
27
     *
28
     * @param array $where Selection WHERE statement.
29
     * @return DocumentSelector
30
     */
31
    public static function find($where = [])
32
    {
33
        return static::source()->find($where);
34
    }
35
36
    /**
37
     * Fetch one record based on provided query or return null. Use second argument to specify
38
     * relations to be loaded.
39
     *
40
     * Example:
41
     * User::findOne(['name' => 'Wolfy-J'], ['profile'], ['id' => 'DESC']);
42
     *
43
     * @param array $where  Selection WHERE statement.
44
     * @param array $sortBy Sort by.
45
     * @return RecordEntity|null
46
     */
47
    public static function findOne($where = [], array $sortBy = [])
48
    {
49
        return static::source()->findOne($where, $sortBy);
50
    }
51
52
    /**
53
     * Find record using it's primary key. Relation data can be preloaded with found record.
54
     *
55
     * Example:
56
     * User::findByID(1, ['profile']);
57
     *
58
     * @param mixed $primaryKey Primary key.
59
     * @return Document|null
60
     */
61
    public static function findByPK($primaryKey)
62
    {
63
        return static::source()->findByPK($primaryKey);
64
    }
65
66
    /**
67
     * Instance of ORM Selector associated with specific document.
68
     *
69
     * @see   Component::staticContainer()
70
     * @param ODM $odm ODM component, global container will be called if not instance provided.
71
     * @return DocumentSource
72
     * @throws ORMException
73
     */
74 View Code Duplication
    public static function source(ODM $odm = null)
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...
75
    {
76
        /**
77
         * Using global container as fallback.
78
         *
79
         * @var ODM $odm
80
         */
81
        if (empty($odm)) {
82
            //Using global container as fallback
83
            $odm = self::staticContainer()->get(ODM::class);
84
        }
85
86
        return $odm->source(static::class);
87
    }
88
}