File media.php
has 2347 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
/**
* WordPress Administration Media API.
*
* @package WordPress
Function get_media_item
has a Cognitive Complexity of 73 (exceeds 5 allowed). Consider refactoring. Open
function get_media_item( $attachment_id, $args = null ) {
global $redir_tab;
$thumb_url = false;
$attachment_id = (int) $attachment_id;
- 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 get_compat_media_markup
has a Cognitive Complexity of 68 (exceeds 5 allowed). Consider refactoring. Open
function get_compat_media_markup( $attachment_id, $args = null ) {
$post = get_post( $attachment_id );
$default_args = array(
'errors' => null,
- 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 media_upload_form_handler
has a Cognitive Complexity of 56 (exceeds 5 allowed). Consider refactoring. Open
function media_upload_form_handler() {
check_admin_referer( 'media-form' );
$errors = null;
- 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 get_media_item
has 208 lines of code (exceeds 25 allowed). Consider refactoring. Open
function get_media_item( $attachment_id, $args = null ) {
global $redir_tab;
$thumb_url = false;
$attachment_id = (int) $attachment_id;
Function media_handle_upload
has a Cognitive Complexity of 48 (exceeds 5 allowed). Consider refactoring. Open
function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
$time = current_time( 'mysql' );
$post = get_post( $post_id );
if ( $post ) {
- 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 attachment_submitbox_metadata
has a Cognitive Complexity of 44 (exceeds 5 allowed). Consider refactoring. Open
function attachment_submitbox_metadata() {
$post = get_post();
$attachment_id = $post->ID;
$file = get_attached_file( $attachment_id );
- 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 media_upload_library_form
has 159 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_upload_library_form( $errors ) {
global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
media_upload_header();
Method attachment_submitbox_metadata
has 157 lines of code (exceeds 25 allowed). Consider refactoring. Open
function attachment_submitbox_metadata() {
$post = get_post();
$attachment_id = $post->ID;
$file = get_attached_file( $attachment_id );
Function wp_media_upload_handler
has a Cognitive Complexity of 40 (exceeds 5 allowed). Consider refactoring. Open
function wp_media_upload_handler() {
$errors = array();
$id = 0;
if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) {
- 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 get_compat_media_markup
has 136 lines of code (exceeds 25 allowed). Consider refactoring. Open
function get_compat_media_markup( $attachment_id, $args = null ) {
$post = get_post( $attachment_id );
$default_args = array(
'errors' => null,
Method media_upload_gallery_form
has 133 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_upload_gallery_form( $errors ) {
global $redir_tab, $type;
$redir_tab = 'gallery';
media_upload_header();
Function media_upload_library_form
has a Cognitive Complexity of 34 (exceeds 5 allowed). Consider refactoring. Open
function media_upload_library_form( $errors ) {
global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
media_upload_header();
- 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 media_upload_form
has 124 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_upload_form( $errors = null ) {
global $type, $tab;
if ( ! _device_can_upload() ) {
echo '<p>' . sprintf(
Method edit_form_image_editor
has 122 lines of code (exceeds 25 allowed). Consider refactoring. Open
function edit_form_image_editor( $post ) {
$open = isset( $_GET['image-editor'] );
if ( $open ) {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
Method media_handle_upload
has 98 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
$time = current_time( 'mysql' );
$post = get_post( $post_id );
if ( $post ) {
Method media_upload_type_url_form
has 96 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_upload_type_url_form( $type = null, $errors = null, $id = null ) {
if ( null === $type ) {
$type = 'image';
}
Method get_attachment_fields_to_edit
has 87 lines of code (exceeds 25 allowed). Consider refactoring. Open
function get_attachment_fields_to_edit( $post, $errors = null ) {
if ( is_int( $post ) ) {
$post = get_post( $post );
}
Method wp_media_insert_url_form
has 85 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_media_insert_url_form( $default_view = 'image' ) {
/** This filter is documented in wp-admin/includes/media.php */
if ( ! apply_filters( 'disable_captions', '' ) ) {
$caption = '
<tr class="image-only">
Function edit_form_image_editor
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
function edit_form_image_editor( $post ) {
$open = isset( $_GET['image-editor'] );
if ( $open ) {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
- 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 wp_add_id3_tag_data
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
function wp_add_id3_tag_data( &$metadata, $data ) {
foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
if ( ! empty( $data[ $version ]['comments'] ) ) {
foreach ( $data[ $version ]['comments'] as $key => $list ) {
if ( 'length' !== $key && ! empty( $list ) ) {
- 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 media_upload_form_handler
has 75 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_upload_form_handler() {
check_admin_referer( 'media-form' );
$errors = null;
Function wp_read_video_metadata
has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring. Open
function wp_read_video_metadata( $file ) {
if ( ! file_exists( $file ) ) {
return false;
}
- 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 wp_media_upload_handler
has 68 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_media_upload_handler() {
$errors = array();
$id = 0;
if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) {
Method wp_read_video_metadata
has 65 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_read_video_metadata( $file ) {
if ( ! file_exists( $file ) ) {
return false;
}
Function get_attachment_fields_to_edit
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
function get_attachment_fields_to_edit( $post, $errors = null ) {
if ( is_int( $post ) ) {
$post = get_post( $post );
}
- 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 wp_media_attach_action
has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring. Open
function wp_media_attach_action( $parent_id, $action = 'attach' ) {
global $wpdb;
if ( ! $parent_id ) {
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
Function get_media_items
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring. Open
function get_media_items( $post_id, $errors ) {
$attachments = array();
if ( $post_id ) {
$post = get_post( $post_id );
- 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 media_handle_sideload
has 48 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) {
$overrides = array( 'test_form' => false );
if ( isset( $post_data['post_date'] ) && substr( $post_data['post_date'], 0, 4 ) > 0 ) {
$time = $post_data['post_date'];
Method wp_iframe
has 47 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_iframe( $content_func, ...$args ) {
global $body_id;
_wp_admin_html_begin();
?>
Function media_handle_sideload
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) {
$overrides = array( 'test_form' => false );
if ( isset( $post_data['post_date'] ) && substr( $post_data['post_date'], 0, 4 ) > 0 ) {
$time = $post_data['post_date'];
- 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 media_upload_type_form
has 42 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_upload_type_form( $type = 'file', $errors = null, $id = null ) {
media_upload_header();
$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0;
Method image_size_input_fields
has 41 lines of code (exceeds 25 allowed). Consider refactoring. Open
function image_size_input_fields( $post, $check = '' ) {
/**
* Filters the names and labels of the default image sizes.
*
* @since 3.3.0
Method wp_read_audio_metadata
has 41 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_read_audio_metadata( $file ) {
if ( ! file_exists( $file ) ) {
return false;
}
Method wp_media_attach_action
has 40 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_media_attach_action( $parent_id, $action = 'attach' ) {
global $wpdb;
if ( ! $parent_id ) {
return;
Function image_size_input_fields
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
function image_size_input_fields( $post, $check = '' ) {
/**
* Filters the names and labels of the default image sizes.
*
* @since 3.3.0
- 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 media_sideload_image
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
function media_sideload_image( $file, $post_id = 0, $desc = null, $return_type = 'html' ) {
if ( ! empty( $file ) ) {
$allowed_extensions = array( 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp' );
- 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 the_media_upload_tabs
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
function the_media_upload_tabs() {
global $redir_tab;
$tabs = media_upload_tabs();
$default = 'type';
- 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 wp_read_audio_metadata
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
function wp_read_audio_metadata( $file ) {
if ( ! file_exists( $file ) ) {
return false;
}
- 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 media_sideload_image
has 35 lines of code (exceeds 25 allowed). Consider refactoring. Open
function media_sideload_image( $file, $post_id = 0, $desc = null, $return_type = 'html' ) {
if ( ! empty( $file ) ) {
$allowed_extensions = array( 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp' );
Method get_media_items
has 33 lines of code (exceeds 25 allowed). Consider refactoring. Open
function get_media_items( $post_id, $errors ) {
$attachments = array();
if ( $post_id ) {
$post = get_post( $post_id );
Method wp_add_id3_tag_data
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_add_id3_tag_data( &$metadata, $data ) {
foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
if ( ! empty( $data[ $version ]['comments'] ) ) {
foreach ( $data[ $version ]['comments'] as $key => $list ) {
if ( 'length' !== $key && ! empty( $list ) ) {
Function media_upload_form
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
function media_upload_form( $errors = null ) {
global $type, $tab;
if ( ! _device_can_upload() ) {
echo '<p>' . sprintf(
- 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 the_media_upload_tabs
has 31 lines of code (exceeds 25 allowed). Consider refactoring. Open
function the_media_upload_tabs() {
global $redir_tab;
$tabs = media_upload_tabs();
$default = 'type';
Method wp_get_media_creation_timestamp
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
function wp_get_media_creation_timestamp( $metadata ) {
$creation_date = false;
if ( empty( $metadata['fileformat'] ) ) {
return $creation_date;
Method get_image_send_to_editor
has 8 arguments (exceeds 4 allowed). Consider refactoring. Open
function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
Method image_add_caption
has 8 arguments (exceeds 4 allowed). Consider refactoring. Open
function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
Function wp_get_media_creation_timestamp
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
function wp_get_media_creation_timestamp( $metadata ) {
$creation_date = false;
if ( empty( $metadata['fileformat'] ) ) {
return $creation_date;
- 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 wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
Avoid too many return
statements within this method. Open
return $src;
Avoid too many return
statements within this method. Open
return $html;
Avoid too many return
statements within this method. Open
return new WP_Error( 'image_sideload_failed' );
Function wp_iframe
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
function wp_iframe( $content_func, ...$args ) {
global $body_id;
_wp_admin_html_begin();
?>
- 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 get_image_send_to_editor
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
$html = get_image_tag( $id, $alt, '', $align, $size );
if ( $rel ) {
- 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 3 locations. Consider refactoring. Open
if ( isset( $attachment['image_alt'] ) ) {
$image_alt = wp_unslash( $attachment['image_alt'] );
if ( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) !== $image_alt ) {
$image_alt = wp_strip_all_tags( $image_alt, true );
- 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 97.
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