File MimeAnalyzer.php
has 629 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
Function doGuessMimeType
has a Cognitive Complexity of 58 (exceeds 5 allowed). Consider refactoring. Open
private function doGuessMimeType( string $file ) {
// Read a chunk of the file
AtEase::suppressWarnings();
$f = fopen( $file, 'rb' );
AtEase::restoreWarnings();
- 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 doGuessMimeType
has 173 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function doGuessMimeType( string $file ) {
// Read a chunk of the file
AtEase::suppressWarnings();
$f = fopen( $file, 'rb' );
AtEase::restoreWarnings();
Function getMediaType
has a Cognitive Complexity of 26 (exceeds 5 allowed). Consider refactoring. Open
public function getMediaType( string $path = null, string $mime = null ): string {
if ( !$mime && !$path ) {
return MEDIATYPE_UNKNOWN;
}
- 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 parseMimeInfo
has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring. Open
protected function parseMimeInfo( string $rawMimeInfo ): void {
$rawMimeInfo = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeInfo );
$rawMimeInfo = str_replace( "\t", " ", $rawMimeInfo );
$lines = explode( "\n", $rawMimeInfo );
- 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
MimeAnalyzer
has 27 functions (exceeds 20 allowed). Consider refactoring. Open
class MimeAnalyzer implements LoggerAwareInterface {
/** @var string */
protected $typeFile;
/** @var string */
protected $infoFile;
Method getMediaType
has 57 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function getMediaType( string $path = null, string $mime = null ): string {
if ( !$mime && !$path ) {
return MEDIATYPE_UNKNOWN;
}
Function loadFiles
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring. Open
protected function loadFiles(): void {
# Allow media handling extensions adding MIME-types and MIME-info
if ( $this->initCallback ) {
call_user_func( $this->initCallback, $this );
}
- 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 detectMimeType
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
private function detectMimeType( string $file, $ext = true ): string {
/** @todo Make $ext default to false. Or better, remove it. */
if ( $ext ) {
$this->logger->info( __METHOD__ .
": WARNING: use of the \$ext parameter is deprecated. "
- 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 parseMimeInfo
has 41 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected function parseMimeInfo( string $rawMimeInfo ): void {
$rawMimeInfo = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeInfo );
$rawMimeInfo = str_replace( "\t", " ", $rawMimeInfo );
$lines = explode( "\n", $rawMimeInfo );
Function improveTypeFromExtension
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public function improveTypeFromExtension( string $mime, string $ext ): ?string {
if ( $mime === 'unknown/unknown' ) {
if ( $this->isRecognizableExtension( $ext ) ) {
$this->logger->info( __METHOD__ . ': refusing to guess mime type for .' .
"$ext file, we should have recognized it" );
- 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 parseMimeTypes
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
protected function parseMimeTypes( string $rawMimeTypes ): void {
$rawMimeTypes = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeTypes );
$rawMimeTypes = str_replace( "\t", " ", $rawMimeTypes );
$lines = explode( "\n", $rawMimeTypes );
- 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 detectMimeType
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
private function detectMimeType( string $file, $ext = true ): string {
/** @todo Make $ext default to false. Or better, remove it. */
if ( $ext ) {
$this->logger->info( __METHOD__ .
": WARNING: use of the \$ext parameter is deprecated. "
Method loadFiles
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
protected function loadFiles(): void {
# Allow media handling extensions adding MIME-types and MIME-info
if ( $this->initCallback ) {
call_user_func( $this->initCallback, $this );
}
Function findMediaType
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
private function findMediaType( string $extMime ) {
if ( strpos( $extMime, '.' ) === 0 ) {
// If it's an extension, look up the MIME types
$m = $this->getMimeTypesFromExtension( substr( $extMime, 1 ) );
if ( !$m ) {
- 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 detectZipTypeFromFile
has 31 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function detectZipTypeFromFile( $handle ) {
$types = [];
$status = ZipDirectoryReader::readHandle(
$handle,
static function ( $entry ) use ( &$types ) {
Method improveTypeFromExtension
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public function improveTypeFromExtension( string $mime, string $ext ): ?string {
if ( $mime === 'unknown/unknown' ) {
if ( $this->isRecognizableExtension( $ext ) ) {
$this->logger->info( __METHOD__ . ': refusing to guess mime type for .' .
"$ext file, we should have recognized it" );
Function detectZipTypeFromFile
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public function detectZipTypeFromFile( $handle ) {
$types = [];
$status = ZipDirectoryReader::readHandle(
$handle,
static function ( $entry ) use ( &$types ) {
- 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
Consider simplifying this complex logical expression. Open
if ( ( strpos( $head, '<?php' ) !== false ) ||
( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
( strpos( $head, "<\x00?\x00 " ) !== false ) ||
( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
Consider simplifying this complex logical expression. Open
if ( str_starts_with( $data, "webm" ) ) {
// XXX HACK look for a video track, if we don't find it, this is an audio file
// This detection is very naive and doesn't parse the actual fields
// 0x86 byte indicates start of codecname field
// next byte is a variable length integer (vint) for the size of the value following it
Avoid too many return
statements within this method. Open
return "unknown/unknown";
Avoid too many return
statements within this method. Open
return 'application/sla';
Avoid too many return
statements within this method. Open
return "image/webp";
Avoid too many return
statements within this method. Open
return 'image/jpx';
Avoid too many return
statements within this method. Open
return $this->detectMicrosoftBinaryType( $f );
Avoid too many return
statements within this method. Open
return $mime;
Avoid too many return
statements within this method. Open
return 'application/sla';
Avoid too many return
statements within this method. Open
return $mime;
Avoid too many return
statements within this method. Open
return $type;
Avoid too many return
statements within this method. Open
return $type;
Avoid too many return
statements within this method. Open
return $xmlTypes[$xml->getRootElement()] ?? 'application/xml';
Avoid too many return
statements within this method. Open
return $this->detectZipTypeFromFile( $f );
Avoid too many return
statements within this method. Open
return $mime;
Avoid too many return
statements within this method. Open
return MEDIATYPE_AUDIO;
Avoid too many return
statements within this method. Open
return $type;
Avoid too many return
statements within this method. Open
return "audio/webm";
Avoid too many return
statements within this method. Open
return 'image/jp2';
Avoid too many return
statements within this method. Open
return MEDIATYPE_AUDIO;
Avoid too many return
statements within this method. Open
return MEDIATYPE_MULTIMEDIA;
Avoid too many return
statements within this method. Open
return "video/webm";
Avoid too many return
statements within this method. Open
return 'application/x-php';
Avoid too many return
statements within this method. Open
return $type;
Avoid too many return
statements within this method. Open
return MEDIATYPE_AUDIO;