| Conditions | 23 |
| Paths | 20 |
| Total Lines | 57 |
| Code Lines | 33 |
| 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 |
||
| 119 | private function processPost() |
||
| 120 | { |
||
| 121 | $data = json_decode($this->request->getContent(), true); |
||
| 122 | |||
| 123 | if (empty($data) || |
||
| 124 | !isset($data['userId']) || |
||
| 125 | !isset($data['gradingProgress']) || |
||
| 126 | !isset($data['activityProgress']) || |
||
| 127 | !isset($data['timestamp']) || |
||
| 128 | (isset($data['timestamp']) && !ImsLti::validateFormatDateIso8601($data['timestamp'])) || |
||
| 129 | (isset($data['scoreGiven']) && !is_numeric($data['scoreGiven'])) || |
||
| 130 | (isset($data['scoreGiven']) && !isset($data['scoreMaximum'])) || |
||
| 131 | (isset($data['scoreMaximum']) && !is_numeric($data['scoreMaximum'])) |
||
| 132 | ) { |
||
| 133 | throw new BadRequestHttpException('Missing data to create score.'); |
||
| 134 | } |
||
| 135 | |||
| 136 | $student = api_get_user_entity($data['userId']); |
||
| 137 | |||
| 138 | if (!$student) { |
||
| 139 | throw new BadRequestHttpException("User (id: {$data['userId']}) not found."); |
||
| 140 | } |
||
| 141 | |||
| 142 | $data['scoreMaximum'] = isset($data['scoreMaximum']) ? $data['scoreMaximum'] : 1; |
||
| 143 | |||
| 144 | $evaluation = $this->lineItem->getEvaluation(); |
||
| 145 | |||
| 146 | $result = Database::getManager() |
||
| 147 | ->getRepository('ChamiloCoreBundle:GradebookResult') |
||
| 148 | ->findOneBy( |
||
| 149 | [ |
||
| 150 | 'userId' => $data['userId'], |
||
| 151 | 'evaluationId' => $evaluation->getId(), |
||
| 152 | ] |
||
| 153 | ); |
||
| 154 | |||
| 155 | if ($result && $result->getCreatedAt() >= new DateTime($data['timestamp'])) { |
||
| 156 | throw new ConflictHttpException('The timestamp on record is later than the incoming score.'); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (isset($data['scoreGiven'])) { |
||
| 160 | if (self::GRADING_FULLY_GRADED !== $data['gradingProgress']) { |
||
| 161 | $data['scoreGiven'] = null; |
||
| 162 | } else { |
||
| 163 | $data['scoreGiven'] = (float) $data['scoreGiven']; |
||
| 164 | |||
| 165 | if ($data['scoreMaximum'] > 0 && $data['scoreMaximum'] != $evaluation->getMax()) { |
||
| 166 | $data['scoreGiven'] = $data['scoreGiven'] * $evaluation->getMax() / $data['scoreMaximum']; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | if (!$result) { |
||
| 172 | $this->response->setStatusCode(Response::HTTP_CREATED); |
||
| 173 | } |
||
| 174 | |||
| 175 | $this->saveScore($data, $student, $result); |
||
| 176 | } |
||
| 215 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: