| Conditions | 5 |
| Paths | 7 |
| Total Lines | 76 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 29 | function displayForm(int $lpViewId) |
||
| 30 | { |
||
| 31 | $em = Database::getManager(); |
||
| 32 | |||
| 33 | $lpView = $em->find(CLpView::class, $lpViewId); |
||
| 34 | |||
| 35 | if (null === $lpView) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | $lp = $em->find(CLp::class, $lpView->getLpId()); |
||
| 40 | |||
| 41 | $extraField = new ExtraField('lp_view'); |
||
| 42 | $field = $extraField->get_handler_field_info_by_field_variable(StudentFollowPage::VARIABLE_ACQUISITION); |
||
| 43 | |||
| 44 | $extraFieldValue = new ExtraFieldValue('lp_view'); |
||
| 45 | $value = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 46 | $lpViewId, |
||
| 47 | StudentFollowPage::VARIABLE_ACQUISITION |
||
| 48 | ); |
||
| 49 | |||
| 50 | $options = []; |
||
| 51 | |||
| 52 | foreach ($field['options'] as $option) { |
||
| 53 | $options[$option['option_value']] = $option['display_text']; |
||
| 54 | } |
||
| 55 | |||
| 56 | $frmId = 'frm_lp_acquisition_'.$lpView->getLpId(); |
||
| 57 | $frmAction = api_get_self().'?'.http_build_query(['lp_view' => $lpViewId, 'a' => 'form_adquisition']); |
||
| 58 | |||
| 59 | $form = new FormValidator($frmId, 'post', $frmAction); |
||
| 60 | $form->addRadio(StudentFollowPage::VARIABLE_ACQUISITION, get_lang('Acquisition'), $options); |
||
| 61 | $form->addHidden('lp_view', $lpViewId); |
||
| 62 | $form->addButtonSave(get_lang('Save')); |
||
| 63 | |||
| 64 | if ($form->validate()) { |
||
| 65 | $values = $form->exportValues(); |
||
| 66 | |||
| 67 | $extraFieldValue = new ExtraFieldValue('lp_view'); |
||
| 68 | $extraFieldValue->save( |
||
| 69 | [ |
||
| 70 | 'variable' => StudentFollowPage::VARIABLE_ACQUISITION, |
||
| 71 | 'item_id' => $lpViewId, |
||
| 72 | 'comment' => json_encode(['user' => api_get_user_id(), 'datetime' => api_get_utc_datetime()]), |
||
| 73 | 'value' => $values[StudentFollowPage::VARIABLE_ACQUISITION], |
||
| 74 | ] |
||
| 75 | ); |
||
| 76 | |||
| 77 | echo StudentFollowPage::getLpAcquisition( |
||
| 78 | [ |
||
| 79 | 'iid' => $lp->getIid(), |
||
| 80 | 'lp_name' => $lp->getName(), |
||
| 81 | ], |
||
| 82 | $lpView->getUserId(), |
||
| 83 | $lpView->getCId(), |
||
| 84 | $lpView->getSessionId(), |
||
| 85 | true |
||
| 86 | ); |
||
| 87 | exit; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (!empty($value)) { |
||
| 91 | $form->setDefaults([StudentFollowPage::VARIABLE_ACQUISITION => $value['value']]); |
||
| 92 | } |
||
| 93 | |||
| 94 | echo $form->returnForm() |
||
| 95 | ."<script>$(function () { |
||
| 96 | $('#$frmId').on('submit', function (e) { |
||
| 97 | e.preventDefault(); |
||
| 98 | |||
| 99 | var self = $(this); |
||
| 100 | |||
| 101 | self.find(':submit').prop('disabled', true); |
||
| 102 | |||
| 103 | $.post(this.action, self.serialize()).done(function (response) { |
||
| 104 | $('#acquisition-$lpViewId').html(response); |
||
| 105 | |||
| 152 |
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: