Completed
Push — master ( 9fbffa...274cae )
by Peter
06:32
created

AbstractFinder::isWithCursor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Maslosoft\Mangan\Abstracts;
4
5
use Iterator;
6
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
7
use Maslosoft\Mangan\Cursor;
8
use Maslosoft\Mangan\Finder;
9
use Maslosoft\Mangan\Helpers\PkManager;
10
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface;
11
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
12
use Maslosoft\Mangan\Interfaces\FinderInterface;
13
use Maslosoft\Mangan\Interfaces\ModelAwareInterface;
14
use Maslosoft\Mangan\Interfaces\ScenariosInterface;
15
use Maslosoft\Mangan\ScenarioManager;
16
use Maslosoft\Mangan\Traits\ModelAwareTrait;
17
use MongoCursor;
18
use UnexpectedValueException;
19
20
/**
21
 * AbstractFinder
22
 *
23
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
24
 */
25
class AbstractFinder implements ModelAwareInterface
26
{
27
28
	use ModelAwareTrait;
29
30
	/**
31
	 * Whenever to use cursors
32
	 * @var bool
33
	 */
34
	private $useCursor = false;
35
36
	/**
37
	 * Finds a single Document with the specified condition.
38
	 *
39
	 * @param array|CriteriaInterface $criteria query criteria.
40
	 * @return AnnotatedInterface
41
	 * @Ignored
42
	 */
43 52
	public function find($criteria = null)
44
	{
45 52
		if ($this->getFinderEvents()->beforeFind($this))
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
46
		{
47 52
			$criteria = $this->getScopeManager()->apply($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
48 52
			$data = $this->getAdapter()->findOne($criteria, $criteria->getSelect());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getAdapter() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
49 52
			return $this->populateRecord($data);
50
		}
51
		return null;
52
	}
53
54
	/**
55
	 * Finds document with the specified primary key. Primary key by default
56
	 * is defined by `_id` field. But could be any other. For simple (one column)
57
	 * keys use it's value.
58
	 *
59
	 * For composite use key-value with column names as keys
60
	 * and values for values.
61
	 *
62
	 * Example for simple pk:
63
	 * ```php
64
	 * $pk = '51b616fcc0986e30026d0748'
65
	 * ```
66
	 *
67
	 * Composite pk:
68
	 * ```php
69
	 * $pk = [
70
	 * 		'mainPk' => 1,
71
	 * 		'secondaryPk' => 2
72
	 * ];
73
	 * ```
74
	 *
75
	 * @param mixed $pkValue primary key value. Use array for composite key.
76
	 * @param array|CriteriaInterface $criteria
77
	 * @return AnnotatedInterface|null
78
	 * @Ignored
79
	 */
80 40
	public function findByPk($pkValue, $criteria = null)
81
	{
82 40
		$pkCriteria = $this->getScopeManager()->getNewCriteria($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
83 40
		$pkCriteria->mergeWith(PkManager::prepare($this->getModel(), $pkValue));
84 40
		return $this->find($pkCriteria);
85
	}
86
87
	/**
88
	 * Finds document with the specified attributes.
89
	 * Attributes should be specified as key-value pairs.
90
	 * This allows easier syntax for simple queries.
91
	 *
92
	 * Example:
93
	 * ```php
94
	 * $attributes = [
95
	 * 		'name' => 'John',
96
	 * 		'title' => 'dr'
97
	 * ];
98
	 * ```
99
	 *
100
	 * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value']
101
	 * @return AnnotatedInterface|null
102
	 */
103 2
	public function findByAttributes(array $attributes)
104
	{
105 2
		$criteria = $this->getScopeManager()->getNewCriteria();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
106 2
		foreach ($attributes as $name => $value)
107
		{
108 2
			$criteria->addCond($name, '==', $value);
109
		}
110 2
		return $this->find($criteria);
111
	}
112
113
	/**
114
	 * Finds all documents satisfying the specified condition.
115
	 * See {@link find()} for detailed explanation about $condition and $params.
116
	 *
117
	 * @param array|CriteriaInterface $criteria query criteria.
118
	 * @return AnnotatedInterface[]|Cursor
119
	 */
120 18
	public function findAll($criteria = null)
121
	{
122 18
		if ($this->getFinderEvents()->beforeFind($this))
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
123
		{
124 18
			$criteria = $this->getScopeManager()->apply($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
125 18
			$cursor = $this->getAdapter()->findMany($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getAdapter() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
126
127 18
			assert(is_object($cursor), sprintf('Expected cursor to be compatible object, got %s', gettype($cursor)));
128 18
			assert($cursor instanceof FinderCursorInterface || $cursor instanceof MongoCursor, new UnexpectedValueException(sprintf('Expected `%s` or `%s` got `%s`', FinderCursorInterface::class, MongoCursor::class, get_class($cursor))));
129
130 18
			if ($criteria->getSort() !== null)
131
			{
132 18
				$cursor->sort($criteria->getSort());
133
			}
134 18
			if ($criteria->getLimit() !== null)
135
			{
136 1
				$cursor->limit($criteria->getLimit());
137
			}
138 18
			if ($criteria->getOffset() !== null)
139
			{
140 1
				$cursor->skip($criteria->getOffset());
0 ignored issues
show
Bug introduced by
The method skip does only exist in MongoCursor, but not in Maslosoft\Mangan\Interfa...s\FinderCursorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
141
			}
142 18
			if ($criteria->getSelect())
143
			{
144 1
				$cursor->fields($criteria->getSelect());
0 ignored issues
show
Bug introduced by
The method fields does only exist in MongoCursor, but not in Maslosoft\Mangan\Interfa...s\FinderCursorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
145
			}
146 18
			$this->getProfiler()->cursor($cursor);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getProfiler() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
147 18
			if ($this->isWithCursor())
148
			{
149 1
				return new Cursor($cursor, $this->getModel());
150
			}
151
			else
152
			{
153 17
				return $this->populateRecords($cursor);
154
			}
155
		}
156
		return [];
157
	}
158
159
	/**
160
	 * Finds all documents with the specified attributes.
161
	 *
162
	 * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value']
163
	 * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents
164
	 * @since v1.0
165
	 */
166 1
	public function findAllByAttributes(array $attributes)
167
	{
168 1
		$criteria = $this->getScopeManager()->getNewCriteria();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
169 1
		foreach ($attributes as $name => $value)
170
		{
171 1
			$criteria->$name('==', $value);
172
		}
173
174 1
		return $this->findAll($criteria);
175
	}
176
177
	/**
178
	 * Finds all documents with the specified primary keys.
179
	 * In MongoDB world every document has '_id' unique field, so with this method that
180
	 * field is in use as PK by default.
181
	 * See {@link find()} for detailed explanation about $condition.
182
	 *
183
	 * @param mixed $pkValues primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value).
184
	 * @param array|CriteriaInterface $criteria query criteria.
185
	 * @return AnnotatedInterface[]|Cursor - Array or cursor of Documents
186
	 * @since v1.0
187
	 */
188 7
	public function findAllByPk($pkValues, $criteria = null)
189
	{
190 7
		$pkCriteria = $this->getScopeManager()->getNewCriteria($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
191 7
		PkManager::prepareAll($this->getModel(), $pkValues, $pkCriteria);
192
193 7
		return $this->findAll($pkCriteria);
194
	}
195
196
	/**
197
	 * Counts all documents satisfying the specified condition.
198
	 * See {@link find()} for detailed explanation about $condition and $params.
199
	 * @param array|CriteriaInterface $criteria query criteria.
200
	 * @return integer Count of all documents satisfying the specified condition.
201
	 * @since v1.0
202
	 */
203 19
	public function count($criteria = null)
204
	{
205 19
		if ($this->getFinderEvents()->beforeCount($this))
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
206
		{
207 19
			$criteria = $this->getScopeManager()->apply($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
208 19
			$count = $this->getAdapter()->count($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getAdapter() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
209 19
			$this->getFinderEvents()->afterCount($this);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
210 19
			return $count;
211
		}
212
		return 0;
213
	}
214
215
	/**
216
	 * Counts all documents found by attribute values.
217
	 *
218
	 * Example:
219
	 * ```php
220
	 * $attributes = [
221
	 * 		'name' => 'John',
222
	 * 		'title' => 'dr'
223
	 * ];
224
	 * ```
225
	 *
226
	 * @param mixed[] Array of attributes and values in form of ['attributeName' => 'value']
227
	 * @return int
228
	 * @since v1.2.2
229
	 * @Ignored
230
	 */
231 1
	public function countByAttributes(array $attributes)
232
	{
233 1
		if ($this->getFinderEvents()->beforeCount($this))
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
234
		{
235 1
			$criteria = $this->getScopeManager()->getNewCriteria();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
236 1
			foreach ($attributes as $name => $value)
237
			{
238 1
				$criteria->$name = $value;
239
			}
240
241 1
			$scopedCriteria = $this->getScopeManager()->apply($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
242
243 1
			$count = $this->getAdapter()->count($scopedCriteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getAdapter() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
244 1
			$this->getFinderEvents()->afterCount($this);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
245 1
			return $count;
246
		}
247
		return 0;
248
	}
249
250
	/**
251
	 * Checks whether there is document satisfying the specified condition.
252
	 *
253
	 * @param CriteriaInterface $criteria
254
	 * @return bool
255
	 */
256 9
	public function exists(CriteriaInterface $criteria = null)
257
	{
258 9
		if ($this->getFinderEvents()->beforeExists($this))
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
259
		{
260 9
			$criteria = $this->getScopeManager()->apply($criteria);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
261
262
			//Select only Pk Fields to not fetch possibly large document
263 9
			$pkKeys = PkManager::getPkKeys($this->getModel());
264 9
			if (is_string($pkKeys))
265
			{
266 7
				$pkKeys = [$pkKeys];
267
			}
268 9
			$cursor = $this->getAdapter()->findMany($criteria, $pkKeys);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getAdapter() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
269 9
			$cursor->limit(1);
270
271
			// NOTE: Cannot use count(true) here because of hhvm mongofill compatibility, see:
272
			// https://github.com/mongofill/mongofill/issues/86
273 9
			$exists = ($cursor->count() !== 0);
274 9
			$this->getFinderEvents()->afterExists($this);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
275 9
			return $exists;
276
		}
277
		return false;
278
	}
279
280
	/**
281
	 * Resets all scopes and criteria applied including default scope.
282
	 *
283
	 * @return Finder
284
	 * @since v1.0
285
	 */
286
	public function resetScope()
287
	{
288
		$this->getScopeManager()->reset();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getScopeManager() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
289
		return $this;
290
	}
291
292
	/**
293
	 * Whenever to use cursor
294
	 * @param bool $useCursor
295
	 * @return FinderInterface
296
	 */
297 69
	public function withCursor($useCursor = true)
298
	{
299 69
		$this->useCursor = $useCursor;
300 69
		return $this;
301
	}
302
303 18
	public function isWithCursor()
304
	{
305 18
		return $this->useCursor;
306
	}
307
308
	/**
309
	 * Creates an model with the given attributes.
310
	 * This method is internally used by the find methods.
311
	 * @param mixed[] $data attribute values (column name=>column value)
312
	 * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class.
313
	 * Null is returned if the input data is false.
314
	 * @since v1.0
315
	 */
316 59
	protected function populateRecord($data)
317
	{
318 59
		if ($data !== null)
319
		{
320 58
			$model = $this->createModel($data);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method createModel() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
321 58
			ScenarioManager::setScenario($model, ScenariosInterface::Update);
322 58
			$this->getFinderEvents()->afterFind($this, $model);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Maslosoft\Mangan\Abstracts\AbstractFinder as the method getFinderEvents() does only exist in the following sub-classes of Maslosoft\Mangan\Abstracts\AbstractFinder: Maslosoft\Mangan\Finder, Maslosoft\Mangan\Helpers\RawFinder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
323 58
			return $model;
324
		}
325
		else
326
		{
327 3
			return null;
328
		}
329
	}
330
331
	/**
332
	 * Creates a list of documents based on the input data.
333
	 * This method is internally used by the find methods.
334
	 * @param Iterator|array $cursor Results found to populate active records.
335
	 * @return AnnotatedInterface[] array list of active records.
336
	 * @since v1.0
337
	 */
338 17
	private function populateRecords($cursor)
339
	{
340 17
		$records = array();
341 17
		foreach ($cursor as $data)
342
		{
343 17
			$records[] = $this->populateRecord($data);
344
		}
345 17
		return $records;
346
	}
347
348
}
349