Conditions | 19 |
Paths | 49 |
Total Lines | 42 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
41 | public function getResourceClass($value, string $resourceClass = null, bool $strict = false): string |
||
42 | { |
||
43 | if ($strict && null === $resourceClass) { |
||
44 | throw new InvalidArgumentException('Strict checking is only possible when resource class is specified.'); |
||
45 | } |
||
46 | |||
47 | $actualClass = \is_object($value) && !$value instanceof \Traversable ? $this->getObjectClass($value) : null; |
||
48 | |||
49 | if (null === $actualClass && null === $resourceClass) { |
||
50 | throw new InvalidArgumentException('Resource type could not be determined. Resource class must be specified.'); |
||
51 | } |
||
52 | |||
53 | if (null !== $actualClass && !$this->isResourceClass($actualClass)) { |
||
54 | throw new InvalidArgumentException(sprintf('No resource class found for object of type "%s".', $actualClass)); |
||
55 | } |
||
56 | |||
57 | if (null !== $resourceClass && !$this->isResourceClass($resourceClass)) { |
||
58 | throw new InvalidArgumentException(sprintf('Specified class "%s" is not a resource class.', $resourceClass)); |
||
59 | } |
||
60 | |||
61 | if ($strict && null !== $actualClass && !is_a($actualClass, $resourceClass, true)) { |
||
62 | throw new InvalidArgumentException(sprintf('Object of type "%s" does not match "%s" resource class.', $actualClass, $resourceClass)); |
||
63 | } |
||
64 | |||
65 | $targetClass = $actualClass ?? $resourceClass; |
||
66 | $mostSpecificResourceClass = null; |
||
67 | |||
68 | foreach ($this->resourceNameCollectionFactory->create() as $resourceClassName) { |
||
69 | if (!is_a($targetClass, $resourceClassName, true)) { |
||
70 | continue; |
||
71 | } |
||
72 | |||
73 | if (null === $mostSpecificResourceClass || is_subclass_of($resourceClassName, $mostSpecificResourceClass)) { |
||
|
|||
74 | $mostSpecificResourceClass = $resourceClassName; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | if (null === $mostSpecificResourceClass) { |
||
79 | throw new \LogicException('Unexpected execution flow.'); |
||
80 | } |
||
81 | |||
82 | return $mostSpecificResourceClass; |
||
83 | } |
||
103 |