mheinzerling/php-databaseutils

View on GitHub

Showing 115 of 115 total issues

Similar blocks of code found in 2 locations. Consider refactoring.
Open

<?php
declare(strict_types = 1);

namespace mheinzerling\commons\database\structure\index;

Severity: Major
Found in src/structure/index/LazyIndex.php and 1 other location - About 5 hrs to fix
src/structure/index/LazyUnique.php on lines 1..50

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 201.

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

Further Reading

Similar blocks of code found in 2 locations. Consider refactoring.
Open

<?php
declare(strict_types = 1);

namespace mheinzerling\commons\database\structure\index;

Severity: Major
Found in src/structure/index/LazyUnique.php and 1 other location - About 5 hrs to fix
src/structure/index/LazyIndex.php on lines 1..50

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 201.

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

Further Reading

Function migrate has a Cognitive Complexity of 38 (exceeds 5 allowed). Consider refactoring.
Open

    public function migrate(Migration $migration, Table $before, SqlSetting $setting): void
    {
        //TODO rename
        $meta = [];
        if ($this->engine != $before->engine) $meta[] = "ENGINE=" . $this->engine;
Severity: Minor
Found in src/structure/Table.php - About 5 hrs to fix

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

The class Table has an overall complexity of 76 which is very high. The configured complexity threshold is 50.
Open

class Table
{
    /**
     * @var Database
     */
Severity: Minor
Found in src/structure/Table.php by phpmd

The class TableBuilder has 18 public methods. Consider refactoring TableBuilder to keep number of public methods under 10.
Open

class TableBuilder
{
    /**
     * @var Table
     */

TooManyPublicMethods

Since: 0.1

A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

By default it ignores methods starting with 'get' or 'set'.

Example

Source https://phpmd.org/rules/codesize.html#toomanypublicmethods

The class FieldBuilder has 15 public methods. Consider refactoring FieldBuilder to keep number of public methods under 10.
Open

class FieldBuilder
{
    /**
     * @var Field
     */

TooManyPublicMethods

Since: 0.1

A class with too many public methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

By default it ignores methods starting with 'get' or 'set'.

Example

Source https://phpmd.org/rules/codesize.html#toomanypublicmethods

Function importDump has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
Open

    public static function importDump(\PDO $pdo, string $sqlFile): bool
    {
        $content = file_get_contents($sqlFile);
        $queries = explode(";\n", str_replace("\r", "", $content));
        $pdo->beginTransaction();
Severity: Minor
Found in src/DatabaseUtils.php - About 2 hrs to fix

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 migrate has 48 lines of code (exceeds 25 allowed). Consider refactoring.
Open

    public function migrate(Migration $migration, Table $before, SqlSetting $setting): void
    {
        //TODO rename
        $meta = [];
        if ($this->engine != $before->engine) $meta[] = "ENGINE=" . $this->engine;
Severity: Minor
Found in src/structure/Table.php - About 1 hr to fix

    Function resolveLazyIndexes has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

        public function resolveLazyIndexes(): void
        {
            foreach ($this->indexes as &$index) {
                if (StringUtils::contains(get_class($index), "lazy")) {
                    if ($index instanceof LazyIndex || $index instanceof LazyUnique) {
    Severity: Minor
    Found in src/structure/Table.php - About 1 hr to fix

    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 migrate has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

        public function migrate(Database $before, SqlSetting $setting, array $renames = null /*TODO*/): Migration
        {
            $migration = new Migration();
            if ($this->name != $before->name) $migration->todo("TODO: rename database to >" . $this->name . "< from >" . $before->name . "<");
            $myTables = $this->topoOrder($this->getTables());
    Severity: Minor
    Found in src/structure/Database.php - About 1 hr to fix

    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 complete has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

        public function complete(): DatabaseBuilder
        {
            $this->table->init($this->engine != null ? $this->engine : $this->db->getDefaultEngine(),
                $this->charset != null ? $this->charset : $this->db->getDefaultCharset(),
                $this->collation != null ? $this->collation : $this->db->getDefaultCollation(),
    Severity: Minor
    Found in src/structure/builder/TableBuilder.php - About 1 hr to fix

    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 toCreateSql has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

        public function toCreateSql(SqlSetting $setting): string
        {
            $delimiter = $setting->singleLine ? " " : "\n";
            $sql = 'CREATE TABLE ';
            if ($setting->createTableIfNotExists) $sql .= 'IF NOT EXISTS ';
    Severity: Minor
    Found in src/structure/Table.php - About 1 hr to fix

    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 fromSql has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
    Open

        public static function fromSql(TableBuilder $tb, string $constraint): void
        {
            //TODO more robust
            if (StringUtils::contains($constraint, "FOREIGN KEY", true)) {
                $onUpdate = ReferenceOption::memberByValueWithDefault(StringUtils::findAndRemove($constraint, "@ON UPDATE (RESTRICT|CASCADE|SET NULL|NO ACTION)@i"), null);
    Severity: Minor
    Found in src/structure/index/Index.php - About 1 hr to fix

    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 modifySql has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
    Open

        public function modifySql(Field $before, SqlSetting $setting): ?string
        {
            //TODO rename
            $ta = $this->type;
            $tb = $before->type;
    Severity: Minor
    Found in src/structure/Field.php - About 1 hr to fix

    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 fromDatabase has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

        public static function fromDatabase(\PDO $pdo, DatabaseBuilder $db, string $tableName, string $engine, string $collation, string $currentAutoincrement = null, array $booleanFields = []): void
        {
            $tb = $db->table($tableName)->engine($engine)->charset(explode("_", $collation, 2)[0])->collation($collation)->autoincrement(intval($currentAutoincrement));
    
    
    
    Severity: Minor
    Found in src/structure/builder/TableBuilder.php - About 55 mins to fix

    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 topoOrder has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

        private function topoOrder(array $tables): array
        {
            $result = [];
            while (count($tables)) {
                $added = false;
    Severity: Minor
    Found in src/structure/Database.php - About 55 mins to fix

    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 7 arguments (exceeds 4 allowed). Consider refactoring.
    Open

        public function __construct(string $tableName, array $fieldNames, string $name = null, string $referenceTableName, array $referenceFields, ReferenceOption $onUpdate, ReferenceOption $onDelete)
    Severity: Major
    Found in src/structure/index/LazyForeignKey.php - About 50 mins to fix

      Method fromDatabase has 7 arguments (exceeds 4 allowed). Consider refactoring.
      Open

          public static function fromDatabase(\PDO $pdo, DatabaseBuilder $db, string $tableName, string $engine, string $collation, string $currentAutoincrement = null, array $booleanFields = []): void
      Severity: Major
      Found in src/structure/builder/TableBuilder.php - About 50 mins to fix

        Avoid variables with short names like $tb. Configured minimum length is 3.
        Open

            public static function fromDatabase(TableBuilder $tb, array $fieldRow, array $fkRows, array $booleanFields = []): void

        ShortVariable

        Since: 0.2

        Detects when a field, local, or parameter has a very short name.

        Example

        class Something {
            private $q = 15; // VIOLATION - Field
            public static function main( array $as ) { // VIOLATION - Formal
                $r = 20 + $this->q; // VIOLATION - Local
                for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
                    $r += $this->q;
                }
            }
        }

        Source https://phpmd.org/rules/naming.html#shortvariable

        Avoid variables with short names like $tb. Configured minimum length is 3.
        Open

                $tb = $db->table($tableName)->engine($engine)->charset(explode("_", $collation, 2)[0])->collation($collation)->autoincrement(intval($currentAutoincrement));

        ShortVariable

        Since: 0.2

        Detects when a field, local, or parameter has a very short name.

        Example

        class Something {
            private $q = 15; // VIOLATION - Field
            public static function main( array $as ) { // VIOLATION - Formal
                $r = 20 + $this->q; // VIOLATION - Local
                for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR)
                    $r += $this->q;
                }
            }
        }

        Source https://phpmd.org/rules/naming.html#shortvariable

        Severity
        Category
        Status
        Source
        Language