Completed
Push — master ( c6463e...8a62ed )
by Pavel
02:21
created

addAggregationFunction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
ccs 0
cts 13
cp 0
cc 5
eloc 13
nc 5
nop 2
crap 30
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 1
{
17
18
	/**
19
	 * @var IAggregationFunction[]
20
	 */
21
	private $aggregationFunctions = [];
22
23
	/**
24
	 * @var IAggregationFunction|NULL
25
	 */
26
	private $multipleAggregationFunction;
27
28
29
	/**
30
	 * @param string               $key
31
	 * @param IAggregationFunction $aggregationFunction
32
	 * @return static
33
	 * @throws DataGridException
34
	 */
35
	public function addAggregationFunction($key, IAggregationFunction $aggregationFunction)
36
	{
37
		if ($this->hasColumnsSummary()) {
0 ignored issues
show
Bug introduced by
It seems like hasColumnsSummary() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
38
			throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions');
39
		}
40
41
		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...
42
			throw new DataGridException('You have to set a data source first.');
43
		}
44
45
		if (isset($this->aggregationFunctions[$key])) {
46
			throw new DataGridException(
47
				"There is already a AggregationFunction defined on column {$key}"
48
			);
49
		}
50
51
		if ($this->multipleAggregationFunction instanceof MultipleAggregationFunction) {
0 ignored issues
show
Bug introduced by
The class Ublaboo\DataGrid\Aggrega...ipleAggregationFunction does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
52
			throw new DataGridException(
53
				"You can not use both AggregationFunctions and MultipleAggregationFunction"
54
			);
55
		}
56
57
		$this->aggregationFunctions[$key] = $aggregationFunction;
58
59
		return $this;
60
	}
61
62
63
	/**
64
	 * @param IMultipleAggregationFunction $multipleAggregationFunction
65
	 */
66
	public function setMultipleAggregationFunction(IMultipleAggregationFunction $multipleAggregationFunction)
67
	{
68
		if ($this->hasColumnsSummary()) {
0 ignored issues
show
Bug introduced by
It seems like hasColumnsSummary() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
			throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions');
70
		}
71
72
		if (!empty($this->aggregationFunctions)) {
73
			throw new DataGridException(
74
				"You can not use both AggregationFunctions and MultipleAggregationFunction"
75
			);
76
		}
77
78
		$this->multipleAggregationFunction = $multipleAggregationFunction;
0 ignored issues
show
Documentation Bug introduced by
It seems like $multipleAggregationFunction of type object<Ublaboo\DataGrid\...pleAggregationFunction> is incompatible with the declared type object<Ublaboo\DataGrid\...gregationFunction>|null of property $multipleAggregationFunction.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
80
		return $this;
81
	}
82
83
84
	/**
85
	 * @param  IDataSource $dataSource
86
	 * @return void
87
	 */
88 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...
89
	{
90 1
		if ($this->multipleAggregationFunction) {
91
			if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) {
92
				$dataSource->processAggregation([$this->multipleAggregationFunction, '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...
93
			}
94
95
			return;
96
		}
97
98 1
		foreach ($this->aggregationFunctions as $aggregationFunction) {
99
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) {
100
				$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...
101
			}
102 1
		}
103 1
	}
104
105
106
	/**
107
	 * @param  IDataSource $dataSource
108
	 * @return void
109
	 */
110 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...
111
	{
112 1
		if ($this->multipleAggregationFunction) {
113
			if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) {
114
				$dataSource->processAggregation([$this->multipleAggregationFunction, '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...
115
			}
116
117
			return;
118
		}
119
120 1
		foreach ($this->aggregationFunctions as $aggregationFunction) {
121
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) {
122
				$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...
123
			}
124 1
		}
125 1
	}
126
127
128
	/**
129
	 * @param  IDataSource $dataSource
130
	 * @return void
131
	 */
132 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...
133
	{
134
		if ($this->multipleAggregationFunction) {
135
			if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) {
136
				$dataSource->processAggregation([$this->multipleAggregationFunction, '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...
137
			}
138
139
			return;
140
		}
141
142
		foreach ($this->aggregationFunctions as $aggregationFunction) {
143
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) {
144
				$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...
145
			}
146
		}
147
	}
148
149
150
	/**
151
	 * @return bool
152
	 */
153
	public function hasSomeAggregationFunction()
154
	{
155
		return !empty($this->aggregationFunctions) || $this->multipleAggregationFunction;
156
	}
157
158
159
	/**
160
	 * @return IAggregationFunction[]
161
	 */
162
	public function getAggregationFunctions()
163
	{
164
		return $this->aggregationFunctions;
165
	}
166
167
168
	/**
169
	 * @return MultipleAggregationFunction
170
	 */
171
	public function getMultipleAggregationFunction()
172
	{
173
		return $this->multipleAggregationFunction;
174
	}
175
176
}
177