UseMuffin / Slug

Slugging for CakePHP
MIT License
35 stars 14 forks source link

php 7.4 Trying to access array offset on value of type null in SlugBehavior #43

Open burzum opened 4 years ago

burzum commented 4 years ago

Running our tests on Scrutinizer-ci.com has shown this issue with 7.4

Trying to access array offset on value of type null in [/home/scrutinizer/build/vendor/muffin/slug/src/Model/Behavior/SlugBehavior.php, line 119]
jadb commented 4 years ago
$this->_table->getSchema()->getColumn($this->getConfig('field'))['length']

So unless the configured slug field isn't an actual column in the table, why would TableSchema::getColumn() return null?

bravo-kernel commented 4 years ago

Mr. Jad! 👋🏼

burzum commented 4 years ago

@jadb it is not the field, but the schema from a CakePHP core call, that returns null for the length for whatever reason. We are using CakePHP 3.8.x.

    public function initialize(array $config)
    {
        if (!$this->getConfig('displayField')) {
            $this->setConfig('displayField', $this->_table->getDisplayField());
        }

        if ($this->getConfig('maxLength') === null) {
            //debug($this->_table->getTable());
            //debug($this->getConfig('field'));
            debug($this->_table->getSchema()->getColumn($this->getConfig('field')));
            $this->setConfig(
                'maxLength',
                $this->_table->getSchema()->getColumn($this->getConfig('field'))['length']
            );
        }

        if ($this->getConfig('unique') === true) {
            $this->setConfig('unique', [$this, '_uniqueSlug']);
        }
    }
###########################
\vendor\muffin\slug\src\Model\Behavior\SlugBehavior.php (line 119)
########## DEBUG ##########
[
        'type' => 'string',
        'length' => (int) 255,
        'null' => true,
        'default' => null,
        'collate' => 'utf8_general_ci',
        'comment' => '',
        'precision' => null,
        'fixed' => null
]
###########################
Notice Error: Trying to access array offset on value of type null in [C:\xampp\htdocs\wa3\vendor\muffin\slug\src\Model\Behavior\SlugBehavior.php, line 122]

Line 122 is this line:

$this->_table->getSchema()->getColumn($this->getConfig('field'))['length']

I don't know yet why this is null in the specific case, however, either this needs to be addressed or an exception with a meaningful message should be thrown.

burzum commented 4 years ago

OK, the issue was that one of the old fixture haven't had the slug field. However, the error handling / info for the developer could be improved. I've made some changes to do so in the SlugBehavior.

It is now checking if the field exists and if it has a length. If not it will throw an exception with an error message pointing at the cause.

    public function initialize(array $config)
    {
        if (!$this->getConfig('displayField')) {
            $this->setConfig('displayField', $this->_table->getDisplayField());
        }

        $field = $this->getConfig('field');

        if (!$this->getTable()->hasField($field)) {
            throw new RuntimeException(sprintf(
                'SlugBehavior: Table `%s` is missing field `%s`',
                $this->getTable()->getTable(),
                $field
            ));
        }

        $fieldSchema = $this->_table->getSchema()->getColumn($field);

        if ($this->getConfig('maxLength') === null) {
            if ($fieldSchema['length'] === null) {
                throw new RuntimeException(sprintf(
                    'SlugBehavior: Table `%s.%s` has no length defined',
                    $this->getTable()->getTable(),
                    $field
                ));
            }

            $this->setConfig(
                'maxLength',
                $fieldSchema['length']
            );
        }

        if ($this->getConfig('unique') === true) {
            $this->setConfig('unique', [$this, '_uniqueSlug']);
        }
    }