File FileSystemUtils.php
has 745 lines of code (exceeds 250 allowed). Consider refactoring. Open
<?php
use Admidio\Exception;
/**
FileSystemUtils
has 41 functions (exceeds 20 allowed). Consider refactoring. Open
final class FileSystemUtils
{
public const CONTENT_TYPE_DIRECTORY = 'directory';
public const CONTENT_TYPE_FILE = 'file';
public const CONTENT_TYPE_LINK = 'link';
Function getDirectoryContent
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring. Open
public static function getDirectoryContent(string $directoryPath, bool $recursive = false, bool $fullPath = true, array $includedContentTypes = array(self::CONTENT_TYPE_DIRECTORY, self::CONTENT_TYPE_FILE, self::CONTENT_TYPE_LINK)): array
{
self::checkIsInAllowedDirectories($directoryPath);
if (!is_dir($directoryPath)) {
- 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 createDirectoryIfNotExists
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
public static function createDirectoryIfNotExists(string $directoryPath, array $options = array()): bool
{
self::checkIsInAllowedDirectories($directoryPath);
$defaultOptions = array('mode' => self::DEFAULT_MODE_DIRECTORY, 'modeParents' => self::DEFAULT_MODE_DIRECTORY, 'createDirectoryStructure' => 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
Method convertPermsToString
has 41 lines of code (exceeds 25 allowed). Consider refactoring. Open
private static function convertPermsToString(int $perms): string
{
switch ($perms & 0xF000) {
case 0xC000: // Socket
$info = 's';
Function deleteDirectoryContentIfExists
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public static function deleteDirectoryContentIfExists(string $directoryPath): bool
{
self::checkIsInAllowedDirectories($directoryPath);
if (!is_dir($directoryPath)) {
- 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 checkDirectoryContentTreeCollisions
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
private static function checkDirectoryContentTreeCollisions(array $directoryContentTree1, array $directoryContentTree2, bool $considerDirectoryCollisions = false): bool
{
foreach ($directoryContentTree1 as $directoryContentName => $directoryContentType1) {
if (array_key_exists($directoryContentName, $directoryContentTree2)) {
if ($considerDirectoryCollisions) {
- 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 checkFilePreconditions
has 38 lines of code (exceeds 25 allowed). Consider refactoring. Open
private static function checkFilePreconditions(string $mode, string $oldFilePath, string $newFilePath, array $options = array()): bool
{
$defaultOptions = array('createDirectoryStructure' => true, 'overwrite' => false);
$options = array_merge($defaultOptions, $options);
Method createDirectoryIfNotExists
has 36 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function createDirectoryIfNotExists(string $directoryPath, array $options = array()): bool
{
self::checkIsInAllowedDirectories($directoryPath);
$defaultOptions = array('mode' => self::DEFAULT_MODE_DIRECTORY, 'modeParents' => self::DEFAULT_MODE_DIRECTORY, 'createDirectoryStructure' => true);
Function checkFilePreconditions
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
private static function checkFilePreconditions(string $mode, string $oldFilePath, string $newFilePath, array $options = array()): bool
{
$defaultOptions = array('createDirectoryStructure' => true, 'overwrite' => false);
$options = array_merge($defaultOptions, $options);
- 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 getNormalizedPath
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
public static function getNormalizedPath(string $path): string
{
$path = str_replace('\\', '/', $path); // Replace back-slashes with forward-slashes
$path = preg_replace('/\/+/', '/', $path); // Combine multiple slashes into a single slash
- 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 checkDirectoryPreconditions
has 33 lines of code (exceeds 25 allowed). Consider refactoring. Open
private static function checkDirectoryPreconditions(string $oldDirectoryPath, string $newDirectoryPath, array $options = array()): bool
{
self::checkIsInAllowedDirectories($oldDirectoryPath);
self::checkIsInAllowedDirectories($newDirectoryPath);
Method getDirectoryContent
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function getDirectoryContent(string $directoryPath, bool $recursive = false, bool $fullPath = true, array $includedContentTypes = array(self::CONTENT_TYPE_DIRECTORY, self::CONTENT_TYPE_FILE, self::CONTENT_TYPE_LINK)): array
{
self::checkIsInAllowedDirectories($directoryPath);
if (!is_dir($directoryPath)) {
Function chmodDirectory
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public static function chmodDirectory(string $directoryPath, int $mode = self::DEFAULT_MODE_DIRECTORY, bool $recursive = false, bool $onlyDirectories = true)
{
if (!self::isUnixWithPosix()) {
throw new RuntimeException('"FileSystemUtils::chmodDirectory()" is only available on systems with POSIX support!');
}
- 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 deleteDirectoryContentIfExists
has 30 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function deleteDirectoryContentIfExists(string $directoryPath): bool
{
self::checkIsInAllowedDirectories($directoryPath);
if (!is_dir($directoryPath)) {
Function deleteDirectoryIfExists
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public static function deleteDirectoryIfExists(string $directoryPath, bool $deleteWithContent = false): bool
{
self::checkIsInAllowedDirectories($directoryPath);
if ($directoryPath === self::ROOT_FOLDER) {
- 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 writeFile
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public static function writeFile(string $filePath, string $data, bool $append = false): int
{
self::checkIsInAllowedDirectories($filePath);
$parentDirectoryPath = dirname($filePath);
- 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 checkDirectoryPreconditions
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
private static function checkDirectoryPreconditions(string $oldDirectoryPath, string $newDirectoryPath, array $options = array()): bool
{
self::checkIsInAllowedDirectories($oldDirectoryPath);
self::checkIsInAllowedDirectories($newDirectoryPath);
- 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 deleteDirectoryIfExists
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
public static function deleteDirectoryIfExists(string $directoryPath, bool $deleteWithContent = false): bool
{
self::checkIsInAllowedDirectories($directoryPath);
if ($directoryPath === self::ROOT_FOLDER) {
Function convertPermsToString
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
private static function convertPermsToString(int $perms): string
{
switch ($perms & 0xF000) {
case 0xC000: // Socket
$info = 's';
- 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 doCopyDirectory
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
private static function doCopyDirectory(string $oldDirectoryPath, string $newDirectoryPath)
{
$oldDirectoryContent = self::getDirectoryContent($oldDirectoryPath, false, false);
foreach ($oldDirectoryContent as $entry => $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 isDirectoryEmpty
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public static function isDirectoryEmpty(string $directoryPath): bool
{
self::checkIsInAllowedDirectories($directoryPath);
if (!is_dir($directoryPath)) {
- 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 chmodFile
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public static function chmodFile(string $filePath, int $mode = self::DEFAULT_MODE_FILE)
{
if (!self::isUnixWithPosix()) {
throw new RuntimeException('"FileSystemUtils::chmodFile()" is only available on systems with POSIX support!');
}
- 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
public static function getPathOwnerInfo(string $path): array
{
if (!self::isUnixWithPosix()) {
throw new RuntimeException('"FileSystemUtils::getPathOwnerInfo()" is only available on systems with POSIX support!');
}
- 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 103.
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
Similar blocks of code found in 2 locations. Consider refactoring. Open
public static function getPathGroupInfo(string $path): array
{
if (!self::isUnix()) {
throw new RuntimeException('"FileSystemUtils::getPathGroupInfo()" is only available on systems with POSIX support!');
}
- 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 103.
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