TIdentifiable::hasId()   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
 * TIdentifiable.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 Neomerx\JsonApi\Contracts\Document\DocumentInterface;
20
21
use IPub\JsonAPIClient\Exceptions;
22
23
/**
24
 * Identifiable trait
25
 *
26
 * @package        iPublikuj:JsonAPIClient!
27
 * @subpackage     Objects
28
 *
29
 * @author         Adam Kadlec <[email protected]>
30
 */
31
trait TIdentifiable
32
{
33
	/**
34
	 * @return string
35
	 *
36
	 * @throws Exceptions\RuntimeException if the type member is not present, or is not a string, or is an empty string
37
	 */
38 View Code Duplication
	public function getType() : string
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...
39
	{
40
		if (!$this->has(DocumentInterface::KEYWORD_TYPE)) {
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...
41
			throw new Exceptions\RuntimeException('Type member not present.');
42
		}
43
44
		$type = $this->get(DocumentInterface::KEYWORD_TYPE);
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...
45
46
		if (!is_string($type) || empty($type)) {
47
			throw new Exceptions\RuntimeException('Type member is not a string, or is empty.');
48
		}
49
50
		return $type;
51
	}
52
53
	/**
54
	 * @return bool
55
	 */
56
	public function hasType() : bool
57
	{
58
		return $this->has(DocumentInterface::KEYWORD_TYPE);
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...
59
	}
60
61
	/**
62
	 * @return string
63
	 *
64
	 * @throws Exceptions\RuntimeException if the id member is not present, or is not a string/int, or is an empty string
65
	 */
66 View Code Duplication
	public function getId() : string
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...
67
	{
68
		if (!$this->has(DocumentInterface::KEYWORD_ID)) {
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...
69
			throw new Exceptions\RuntimeException('Id member not present.');
70
		}
71
72
		$id = $this->get(DocumentInterface::KEYWORD_ID);
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...
73
74
		if (!is_string($id)) {
75
			throw new Exceptions\RuntimeException('Id member is not a string.');
76
		}
77
78
		if (empty($id)) {
79
			throw new Exceptions\RuntimeException('Id member is an empty string.');
80
		}
81
82
		return $id;
83
	}
84
85
	/**
86
	 * @return bool
87
	 */
88
	public function hasId() : bool
89
	{
90
		return $this->has(DocumentInterface::KEYWORD_ID);
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...
91
	}
92
}
93