Conditions | 7 |
Paths | 12 |
Total Lines | 53 |
Lines | 6 |
Ratio | 11.32 % |
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 |
||
107 | public function checkConstraint( Context $context, Constraint $constraint ) { |
||
108 | $parameters = []; |
||
109 | $constraintParameters = $constraint->getConstraintParameters(); |
||
110 | $namespace = $this->constraintParameterParser->parseNamespaceParameter( $constraintParameters, $constraint->getConstraintTypeItemId() ); |
||
111 | $parameters['namespace'] = [ $namespace ]; |
||
112 | |||
113 | $snak = $context->getSnak(); |
||
114 | |||
115 | if ( !$snak instanceof PropertyValueSnak ) { |
||
116 | // nothing to check |
||
117 | return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_COMPLIANCE ); |
||
118 | } |
||
119 | |||
120 | $dataValue = $snak->getDataValue(); |
||
121 | |||
122 | /* |
||
123 | * error handling: |
||
124 | * type of $dataValue for properties with 'Commons link' constraint has to be 'string' |
||
125 | * parameter $namespace can be null, works for commons galleries |
||
126 | */ |
||
127 | View Code Duplication | if ( $dataValue->getType() !== 'string' ) { |
|
|
|||
128 | $message = ( new ViolationMessage( 'wbqc-violation-message-value-needed-of-type' ) ) |
||
129 | ->withEntityId( new ItemId( $constraint->getConstraintTypeItemId() ), Role::CONSTRAINT_TYPE_ITEM ) |
||
130 | ->withDataValueType( 'string' ); |
||
131 | return new CheckResult( $context, $constraint, $parameters, CheckResult::STATUS_VIOLATION, $message ); |
||
132 | } |
||
133 | |||
134 | $commonsLink = $dataValue->getValue(); |
||
135 | |||
136 | try { |
||
137 | if ( !$this->commonsLinkIsWellFormed( $commonsLink ) ) { |
||
138 | throw new MalformedTitleException( 'wbqc-violation-message-commons-link-not-well-formed', $commonsLink ); // caught below |
||
139 | } |
||
140 | list( $defaultNamespace, $prefix ) = $this->getCommonsNamespace( $namespace ); |
||
141 | $title = $this->titleParser->parseTitle( $prefix . $commonsLink, $defaultNamespace ); |
||
142 | if ( $this->pageExists( $title ) ) { |
||
143 | $message = null; |
||
144 | $status = CheckResult::STATUS_COMPLIANCE; |
||
145 | } else { |
||
146 | if ( $this->valueIncludesNamespace( $commonsLink, $namespace ) ) { |
||
147 | throw new MalformedTitleException( 'wbqc-violation-message-commons-link-not-well-formed', $commonsLink ); // caught below |
||
148 | } else { |
||
149 | $message = new ViolationMessage( 'wbqc-violation-message-commons-link-no-existent' ); |
||
150 | $status = CheckResult::STATUS_VIOLATION; |
||
151 | } |
||
152 | } |
||
153 | } catch ( MalformedTitleException $e ) { |
||
154 | $message = new ViolationMessage( 'wbqc-violation-message-commons-link-not-well-formed' ); |
||
155 | $status = CheckResult::STATUS_VIOLATION; |
||
156 | } |
||
157 | |||
158 | return new CheckResult( $context, $constraint, $parameters, $status, $message ); |
||
159 | } |
||
160 | |||
221 |
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.