Conditions | 3 |
Paths | 5 |
Total Lines | 56 |
Code Lines | 33 |
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 |
||
51 | public function acquire(): bool |
||
52 | { |
||
53 | $request = [ |
||
54 | 'table_name' => $this->getTable(), |
||
55 | 'condition' => [ |
||
56 | 'row_existence' => RowExistenceExpectationConst::CONST_IGNORE, |
||
57 | 'column_condition' => [ |
||
58 | 'logical_operator' => LogicalOperatorConst::CONST_OR, |
||
59 | 'sub_conditions' => [ |
||
60 | /** (`owner` != $this->owner OR `owner` IS NULL) AND (`expiration` >= time()) */ |
||
61 | [ |
||
62 | 'logical_operator' => LogicalOperatorConst::CONST_AND, |
||
63 | 'sub_conditions' => [ |
||
64 | [ |
||
65 | 'column_name' => 'owner', |
||
66 | 'value' => [$this->owner, ColumnTypeConst::CONST_STRING], |
||
67 | 'comparator' => ComparatorTypeConst::CONST_NOT_EQUAL, |
||
68 | 'pass_if_missing' => true, |
||
69 | 'latest_version_only' => true, |
||
70 | ], |
||
71 | [ |
||
72 | 'column_name' => 'expiration', |
||
73 | 'value' => [$this->currentTime(), ColumnTypeConst::CONST_INTEGER], |
||
74 | 'comparator' => ComparatorTypeConst::CONST_LESS_EQUAL, |
||
75 | 'pass_if_missing' => true, |
||
76 | 'latest_version_only' => true, |
||
77 | ], |
||
78 | ], |
||
79 | ], |
||
80 | |||
81 | /** (`owner` = $this->owner OR `owner` IS NULL) */ |
||
82 | [ |
||
83 | |||
84 | 'column_name' => 'owner', |
||
85 | 'value' => [$this->owner, ColumnTypeConst::CONST_STRING], |
||
86 | 'comparator' => ComparatorTypeConst::CONST_EQUAL, |
||
87 | 'pass_if_missing' => true, |
||
88 | 'latest_version_only' => true, |
||
89 | ], |
||
90 | ], |
||
91 | ], |
||
92 | ], |
||
93 | 'primary_key' => $this->makePrimaryKey($this->name), |
||
94 | 'attribute_columns' => $this->makeAttributeColumns(time(), $this->seconds), |
||
95 | ]; |
||
96 | |||
97 | try { |
||
98 | $response = $this->getOts()->putRow($request); |
||
|
|||
99 | |||
100 | return isset($response['primary_key'], $response['attribute_columns']); |
||
101 | } catch (OTSServerException $exception) { |
||
102 | if ('OTSConditionCheckFail' === $exception->getOTSErrorCode()) { |
||
103 | return false; |
||
104 | } |
||
105 | |||
106 | throw $exception; |
||
107 | } |
||
201 |