| Conditions | 14 |
| Paths | 10 |
| Total Lines | 95 |
| Code Lines | 54 |
| Lines | 10 |
| Ratio | 10.53 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 61 | public function checkRecordOrigin(ConstraintEventSchema $event) |
||
| 62 | { |
||
| 63 | $currentRecord = $this->utils->getCurrentEntity(); |
||
| 64 | $data = $event->getElement(); |
||
| 65 | |||
| 66 | // if no recordorigin set on saved record; we let it through |
||
| 67 | if (is_null($currentRecord) || !isset($currentRecord->{$this->recordOriginField})) { |
||
| 68 | // we have no current record.. but make sure user doesn't want to send the banned recordOrigin |
||
| 69 | if (isset($data->{$this->recordOriginField}) && |
||
| 70 | !is_null($data->{$this->recordOriginField}) && |
||
| 71 | in_array($data->{$this->recordOriginField}, $this->recordOriginBlacklist) |
||
| 72 | ) { |
||
| 73 | $event->addError( |
||
| 74 | sprintf( |
||
| 75 | 'Creating documents with the %s field having a value of %s is not permitted.', |
||
| 76 | $this->recordOriginField, |
||
| 77 | implode(', ', $this->recordOriginBlacklist) |
||
| 78 | ), |
||
| 79 | $this->recordOriginField |
||
| 80 | ); |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | return; |
||
| 85 | } |
||
| 86 | |||
| 87 | $recordOrigin = $currentRecord->{$this->recordOriginField}; |
||
| 88 | |||
| 89 | // not in the blacklist? can also go through.. |
||
| 90 | if (!in_array($recordOrigin, $this->recordOriginBlacklist)) { |
||
| 91 | return; |
||
| 92 | } |
||
| 93 | |||
| 94 | // ok, user is trying to modify an object with blacklist recordorigin.. let's check fields |
||
| 95 | $schema = $event->getSchema(); |
||
| 96 | $isAllowed = true; |
||
| 97 | |||
| 98 | if (!isset($schema->{'x-documentClass'})) { |
||
| 99 | // this should never happen but we need to check. if schema has no information to *check* our rules, we |
||
| 100 | // MUST deny it in that case.. |
||
| 101 | $event->addError( |
||
| 102 | 'Internal error, not enough schema information to validate recordOrigin rules.', |
||
| 103 | $this->recordOriginField |
||
| 104 | ); |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | $documentClass = $schema->{'x-documentClass'}; |
||
| 109 | |||
| 110 | if (!isset($this->exceptionFieldMap[$documentClass])) { |
||
| 111 | // if he wants to edit on blacklist, but we have no exceptions, also deny.. |
||
| 112 | $isAllowed = false; |
||
| 113 | } else { |
||
| 114 | // so to check our exceptions, we remove it from both documents (the stored and the clients) and compare |
||
| 115 | $exceptions = $this->exceptionFieldMap[$documentClass]; |
||
| 116 | |||
| 117 | $accessor = PropertyAccess::createPropertyAccessorBuilder() |
||
| 118 | ->enableMagicCall() |
||
| 119 | ->getPropertyAccessor(); |
||
| 120 | |||
| 121 | $storedObject = clone $currentRecord; |
||
| 122 | $userObject = clone $data; |
||
| 123 | |||
| 124 | foreach ($exceptions as $fieldName) { |
||
| 125 | View Code Duplication | if ($accessor->isWritable($storedObject, $fieldName)) { |
|
|
|
|||
| 126 | $accessor->setValue($storedObject, $fieldName, null); |
||
| 127 | } else { |
||
| 128 | $this->addProperties($fieldName, $storedObject); |
||
| 129 | } |
||
| 130 | View Code Duplication | if ($accessor->isWritable($userObject, $fieldName)) { |
|
| 131 | $accessor->setValue($userObject, $fieldName, null); |
||
| 132 | } else { |
||
| 133 | $this->addProperties($fieldName, $userObject); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // so now all unimportant fields were set to null on both - they should match if rest is untouched ;-) |
||
| 138 | if ($userObject != $storedObject) { |
||
| 139 | $isAllowed = false; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | if (!$isAllowed) { |
||
| 144 | $event->addError( |
||
| 145 | sprintf( |
||
| 146 | 'Prohibited modification attempt on record with %s of %s', |
||
| 147 | $this->recordOriginField, |
||
| 148 | implode(', ', $this->recordOriginBlacklist) |
||
| 149 | ), |
||
| 150 | $this->recordOriginField |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | return; |
||
| 155 | } |
||
| 156 | |||
| 193 |
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.