File HTMLFormField.php
has 762 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
namespace MediaWiki\HTMLForm;
use HtmlArmor;
HTMLFormField
has 59 functions (exceeds 20 allowed). Consider refactoring. Open
abstract class HTMLFormField {
/** @var array|array[] */
public $mParams;
/** @var callable(mixed,array,HTMLForm):(Status|string|bool|Message) */
Function __construct
has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring. Open
public function __construct( $params ) {
$this->mParams = $params;
if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm ) {
$this->mParent = $params['parent'];
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method __construct
has 62 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function __construct( $params ) {
$this->mParams = $params;
if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm ) {
$this->mParent = $params['parent'];
Method getOOUI
has 52 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getOOUI( $value ) {
$inputField = $this->getInputOOUI( $value );
if ( !$inputField ) {
// This field doesn't have an OOUI implementation yet at all. Fall back to getDiv() to
Method getCodex
has 48 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getCodex( $value ) {
$isDisabled = ( $this->mParams['disabled'] ?? false );
// Label
$labelDiv = '';
Method getTableRow
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getTableRow( $value ) {
[ $errors, $errorClass ] = $this->getErrorsAndErrorClass( $value );
$inputHtml = $this->getInputHTML( $value );
$fieldType = $this->getClassName();
$helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
Method validateCondState
has 38 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected function validateCondState( $params ) {
$origParams = $params;
$op = array_shift( $params );
$makeException = function ( string $details ) use ( $origParams ): InvalidArgumentException {
Method getDiv
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getDiv( $value ) {
[ $errors, $errorClass ] = $this->getErrorsAndErrorClass( $value );
$inputHtml = $this->getInputHTML( $value );
$fieldType = $this->getClassName();
$helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
Function validateCondState
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
protected function validateCondState( $params ) {
$origParams = $params;
$op = array_shift( $params );
$makeException = function ( string $details ) use ( $origParams ): InvalidArgumentException {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getOOUI
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public function getOOUI( $value ) {
$inputField = $this->getInputOOUI( $value );
if ( !$inputField ) {
// This field doesn't have an OOUI implementation yet at all. Fall back to getDiv() to
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method checkStateRecurse
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected function checkStateRecurse( array $alldata, array $params ) {
$op = array_shift( $params );
$valueChk = [ 'AND' => false, 'OR' => true, 'NAND' => false, 'NOR' => true ];
$valueRet = [ 'AND' => true, 'OR' => false, 'NAND' => false, 'NOR' => true ];
Function getOptions
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
public function getOptions() {
if ( $this->mOptions === false ) {
if ( array_key_exists( 'options-messages', $this->mParams ) ) {
$needsParse = $this->mParams['options-messages-parse'] ?? false;
if ( $needsParse ) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getCodex
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public function getCodex( $value ) {
$isDisabled = ( $this->mParams['disabled'] ?? false );
// Label
$labelDiv = '';
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getLabelHtml
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public function getLabelHtml( $cellAttributes = [] ) {
# Don't output a for= attribute for labels with no associated input.
# Kind of hacky here, possibly we don't want these to be <label>s at all.
$for = $this->needsLabel() ? [ 'for' => $this->mID ] : [];
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getHelpText
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public function getHelpText() {
$html = [];
foreach ( $this->getHelpMessages() as $msg ) {
if ( $msg instanceof HtmlArmor ) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getAttributes
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public function getAttributes( array $list ) {
static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ];
$ret = [];
foreach ( $list as $key ) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid too many return
statements within this method. Open
return $p;
Avoid too many return
statements within this method. Open
return ( $value !== $testValue );
Function formatErrors
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
protected static function formatErrors( $errors ) {
if ( is_array( $errors ) && count( $errors ) === 1 ) {
$errors = array_shift( $errors );
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function validate
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function validate( $value, $alldata ) {
if ( $this->isHidden( $alldata ) ) {
return true;
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getErrorsRaw
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public function getErrorsRaw( $value ) {
$errors = $this->validate( $value, $this->mParent->mFieldData );
if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
return [];
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Similar blocks of code found in 2 locations. Consider refactoring. Open
if ( isset( $params['label-message'] ) ) {
$this->mLabel = $this->getMessage( $params['label-message'] )->parse();
} elseif ( isset( $params['label'] ) ) {
if ( $params['label'] === ' ' || $params['label'] === "\u{00A0}" ) {
// Apparently some things set   directly and in an odd format
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 123.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76