Completed
Push — master ( de0724...bff5ff )
by Pavel
02:29
created

TDataGridAggregationFunction   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 92
Duplicated Lines 26.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 24
loc 92
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addAggregationFunction() 0 16 3
A beforeDataModelFilter() 8 8 3
A afterDataModelFilter() 8 8 3
A afterDataModelPaginated() 8 8 3
A hasSomeAggregationFunction() 0 4 1
A getAggregationFunctions() 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
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\AggregationFunction;
10
11
use Ublaboo\DataGrid\Exception\DataGridException;
12
use Ublaboo\DataGrid\DataModel;
13
use Ublaboo\DataGrid\DataSource\IDataSource;
14
15
trait TDataGridAggregationFunction
16
{
17
18
	/**
19
	 * @var IAggregationFunction[]
20
	 */
21
	private $aggregationFunctions = [];
22
23
24
	/**
25
	 * @param string               $key
26
	 * @param IAggregationFunction $aggregationFunction
27
	 * @return static
28
	 */
29
	public function addAggregationFunction($key, IAggregationFunction $aggregationFunction)
30
	{
31
		if (!($this->dataModel instanceof DataModel)) {
0 ignored issues
show
Bug introduced by
The property dataModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
			throw new DataGridException('You have to set a data source first.');
33
		}
34
35
		if (isset($this->aggregationFunctions[$key])) {
36
			throw new DataGridException(
37
				"There is already a AggregationFunction defined on column {$key}"
38
			);
39
		}
40
41
		$this->aggregationFunctions[$key] = $aggregationFunction;
42
43
		return $this;
44
	}
45
46
47
	/**
48
	 * @param  IDataSource $dataSource
49
	 * @return void
50
	 */
51 View Code Duplication
	public function beforeDataModelFilter(IDataSource $dataSource)
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...
52
	{
53
		foreach ($this->aggregationFunctions as $aggregationFunction) {
54
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) {
55
				$dataSource->processAggregation([$aggregationFunction, 'processDataSource']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Ublaboo\DataGrid\DataSource\IDataSource as the method processAggregation() does only exist in the following implementations of said interface: Ublaboo\DataGrid\DataSource\DibiFluentDataSource, Ublaboo\DataGrid\DataSou...biFluentMssqlDataSource, Ublaboo\DataGrid\DataSou...DatabaseTableDataSource, Ublaboo\DataGrid\DataSou...aseTableMssqlDataSource.

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...
56
			}
57
		}
58
	}
59
60
61
	/**
62
	 * @param  IDataSource $dataSource
63
	 * @return void
64
	 */
65 View Code Duplication
	public function afterDataModelFilter(IDataSource $dataSource)
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...
66
	{
67
		foreach ($this->aggregationFunctions as $aggregationFunction) {
68
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) {
69
				$dataSource->processAggregation([$aggregationFunction, 'processDataSource']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Ublaboo\DataGrid\DataSource\IDataSource as the method processAggregation() does only exist in the following implementations of said interface: Ublaboo\DataGrid\DataSource\DibiFluentDataSource, Ublaboo\DataGrid\DataSou...biFluentMssqlDataSource, Ublaboo\DataGrid\DataSou...DatabaseTableDataSource, Ublaboo\DataGrid\DataSou...aseTableMssqlDataSource.

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...
70
			}
71
		}
72
	}
73
74
75
	/**
76
	 * @param  IDataSource $dataSource
77
	 * @return void
78
	 */
79 View Code Duplication
	public function afterDataModelPaginated(IDataSource $dataSource)
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...
80
	{
81
		foreach ($this->aggregationFunctions as $aggregationFunction) {
82
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) {
83
				$dataSource->processAggregation([$aggregationFunction, 'processDataSource']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Ublaboo\DataGrid\DataSource\IDataSource as the method processAggregation() does only exist in the following implementations of said interface: Ublaboo\DataGrid\DataSource\DibiFluentDataSource, Ublaboo\DataGrid\DataSou...biFluentMssqlDataSource, Ublaboo\DataGrid\DataSou...DatabaseTableDataSource, Ublaboo\DataGrid\DataSou...aseTableMssqlDataSource.

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...
84
			}
85
		}
86
	}
87
88
89
	/**
90
	 * @return bool
91
	 */
92
	public function hasSomeAggregationFunction()
93
	{
94
		return !empty($this->aggregationFunctions);
95
	}
96
97
98
	/**
99
	 * @return IAggregationFunction[]
100
	 */
101
	public function getAggregationFunctions()
102
	{
103
		return $this->aggregationFunctions;
104
	}
105
106
}
107