propelorm/Propel2

View on GitHub
src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php
B

Complex method in addArchiveTable

    protected function addArchiveTable()
    {
        $table = $this->getTable();
        $database = $table->getDatabase();
        $archiveTableName = $this->getParameter('archive_table') ? $this->getParameter('archive_table') : ($this->getTable()->getOriginCommonName() . '_archive');

Long or complex methods can make code harder to understand. In most circumstances, methods are best as a small chunk of code (the "how") with a clear, understandable name (the "what"). Long methods can also lead to duplication, as it's harder to reuse logic that is tightly coupled to surrounding code.

Refactorings

Read More

Method addArchiveTable is too long

    protected function addArchiveTable()
    {
        $table = $this->getTable();
        $database = $table->getDatabase();
        $archiveTableName = $this->getParameter('archive_table') ? $this->getParameter('archive_table') : ($this->getTable()->getOriginCommonName() . '_archive');
Severity: Minor
Found in src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php - About 55 mins to fix

Class ArchivableBehavior contains too many public methods

class ArchivableBehavior extends Behavior
{
    // default parameters value
    protected $parameters = [
        'archive_table'       => '',
Severity: Minor
Found in src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php - About 40 mins to fix

The variable name, objectBuilderModifier, is too long

    protected $objectBuilderModifier;
Severity: Minor
Found in src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php - About 5 mins to fix

The variable name, b, is too short

            $b = clone $this;
Severity: Minor
Found in src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php - About 5 mins to fix

There are no issues that match your filters.

<?php

/**
 * This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @license MIT License
 */

namespace Propel\Generator\Behavior\Archivable;

use Propel\Generator\Exception\InvalidArgumentException;
use Propel\Generator\Model\Behavior;
use Propel\Generator\Model\Column;
use Propel\Generator\Model\Index;
use Propel\Generator\Model\Table;

/**
 * Keeps tracks of an ActiveRecord object, even after deletion
 *
 * @author Francois Zaninotto
 */
class ArchivableBehavior extends Behavior
{
    // default parameters value
    protected $parameters = [
        'archive_table'       => '',
        'archive_phpname'     => null,
        'archive_class'       => '',
        'log_archived_at'     => 'true',
        'archived_at_column'  => 'archived_at',
        'archive_on_insert'   => 'false',
        'archive_on_update'   => 'false',
        'archive_on_delete'   => 'true',
    ];

    protected $archiveTable;
    protected $objectBuilderModifier;
    protected $queryBuilderModifier;

    public function modifyDatabase()
    {
        foreach ($this->getDatabase()->getTables() as $table) {
            if ($table->hasBehavior($this->getId())) {
                // don't add the same behavior twice
                continue;
            }
            if (property_exists($table, 'isArchiveTable')) {
                // don't add the behavior to archive tables
                continue;
            }
            $b = clone $this;
            $table->addBehavior($b);
        }
    }

    public function modifyTable()
    {
        if ($this->getParameter('archive_class') && $this->getParameter('archive_table')) {
            throw new InvalidArgumentException('Please set only one of the two parameters "archive_class" and "archive_table".');
        }
        if (!$this->getParameter('archive_class')) {
            $this->addArchiveTable();
        }
    }

    protected function addArchiveTable()
    {
        $table = $this->getTable();
        $database = $table->getDatabase();
        $archiveTableName = $this->getParameter('archive_table') ? $this->getParameter('archive_table') : ($this->getTable()->getOriginCommonName() . '_archive');
        if (!$database->hasTable($archiveTableName)) {
            // create the version table
            $archiveTable = $database->addTable([
                'name'      => $archiveTableName,
                'phpName'   => $this->getParameter('archive_phpname'),
                'package'   => $table->getPackage(),
                'schema'    => $table->getSchema(),
                'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null,
                'identifierQuoting' => $table->getIdentifierQuoting()
            ]);
            $archiveTable->isArchiveTable = true;
            // copy all the columns
            foreach ($table->getColumns() as $column) {
                $columnInArchiveTable = clone $column;
                if ($columnInArchiveTable->hasReferrers()) {
                    $columnInArchiveTable->clearReferrers();
                }
                if ($columnInArchiveTable->isAutoincrement()) {
                    $columnInArchiveTable->setAutoIncrement(false);
                }
                $archiveTable->addColumn($columnInArchiveTable);
            }
            // add archived_at column
            if ($this->getParameter('log_archived_at') == 'true') {
                $archiveTable->addColumn([
                    'name' => $this->getParameter('archived_at_column'),
                    'type' => 'TIMESTAMP'
                ]);
            }
            // do not copy foreign keys
            // copy the indices
            foreach ($table->getIndices() as $index) {
                $copiedIndex = clone $index;
                $archiveTable->addIndex($copiedIndex);
            }
            // copy unique indices to indices
            // see https://github.com/propelorm/Propel/issues/175 for details
            foreach ($table->getUnices() as $unique) {
                $index = new Index();
                $index->setTable($table);
                foreach ($unique->getColumns() as $columnName) {
                    if ($size = $unique->getColumnSize($columnName)) {
                        $index->addColumn(['name' => $columnName, 'size' => $size]);
                    } else {
                        $index->addColumn(['name' => $columnName]);
                    }
                }
                $archiveTable->addIndex($index);
            }
            // every behavior adding a table should re-execute database behaviors
            foreach ($database->getBehaviors() as $behavior) {
                $behavior->modifyDatabase();
            }
            $this->archiveTable = $archiveTable;
        } else {
            $this->archiveTable = $database->getTable($archiveTableName);
        }
    }

    /**
     * @return Table
     */
    public function getArchiveTable()
    {
        return $this->archiveTable;
    }

    public function getArchiveTablePhpName($builder)
    {
        if ($this->hasArchiveClass()) {
            return $this->getParameter('archive_class');
        }

        return $builder->getClassNameFromBuilder($builder->getNewStubObjectBuilder($this->getArchiveTable()));
    }

    public function getArchiveTableQueryName($builder)
    {
        if ($this->hasArchiveClass()) {
            return $this->getParameter('archive_class') . 'Query';
        }

        return $builder->getClassNameFromBuilder($builder->getNewStubQueryBuilder($this->getArchiveTable()));
    }

    public function hasArchiveClass()
    {
        return $this->getParameter('archive_class') ? true : false;
    }

    /**
     * @return Column
     */
    public function getArchivedAtColumn()
    {
        if ($this->getArchiveTable() && 'true' === $this->getParameter('log_archived_at')) {
            return $this->getArchiveTable()->getColumn($this->getParameter('archived_at_column'));
        }
    }

    public function isArchiveOnInsert()
    {
        return 'true' === $this->getParameter('archive_on_insert');
    }

    public function isArchiveOnUpdate()
    {
        return 'true' === $this->getParameter('archive_on_update');
    }

    public function isArchiveOnDelete()
    {
        return 'true' === $this->getParameter('archive_on_delete');
    }

    public function getObjectBuilderModifier()
    {
        if (null === $this->objectBuilderModifier) {
            $this->objectBuilderModifier = new ArchivableBehaviorObjectBuilderModifier($this);
        }

        return $this->objectBuilderModifier;
    }

    public function getQueryBuilderModifier()
    {
        if (null === $this->queryBuilderModifier) {
            $this->queryBuilderModifier = new ArchivableBehaviorQueryBuilderModifier($this);
        }

        return $this->queryBuilderModifier;
    }
}

Size

Lines of code
154