TMetaMember::hasMeta()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * TMetaMember.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:JsonAPIClient!
9
 * @subpackage     Objects
10
 * @since          1.0.0
11
 *
12
 * @date           05.05.18
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\JsonAPIClient\Objects;
18
19
use CloudCreativity\Utils\Object\StandardObject;
20
use CloudCreativity\Utils\Object\StandardObjectInterface;
21
22
use Neomerx\JsonApi\Contracts\Document\DocumentInterface;
23
24
use IPub\JsonAPIClient\Exceptions;
25
26
/**
27
 * Meta member trait
28
 *
29
 * @package        iPublikuj:JsonAPIClient!
30
 * @subpackage     Objects
31
 *
32
 * @author         Adam Kadlec <[email protected]>
33
 */
34
trait TMetaMember
35
{
36
	/**
37
	 * Get the meta member of the document.
38
	 *
39
	 * @return StandardObjectInterface
40
	 *
41
	 * @throws Exceptions\RuntimeException if the meta member is present and is not an object or null
42
	 */
43
	public function getMeta() : StandardObjectInterface
44
	{
45
		$meta = $this->hasMeta() ? $this->get(DocumentInterface::KEYWORD_META) : new StandardObject();
0 ignored issues
show
Bug introduced by
It seems like get() 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...
46
47
		if (!is_null($meta) && !$meta instanceof StandardObjectInterface) {
0 ignored issues
show
Bug introduced by
The class CloudCreativity\Utils\Ob...StandardObjectInterface 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...
48
			throw new Exceptions\RuntimeException('Data member is not an object.');
49
		}
50
51
		return $meta;
52
	}
53
54
	/**
55
	 * @return bool
56
	 */
57
	public function hasMeta() : bool
58
	{
59
		return $this->has(DocumentInterface::KEYWORD_META);
0 ignored issues
show
Bug introduced by
It seems like has() 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...
60
	}
61
}
62