Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
burzum opened this issue Feb 26, 2020 · 4 comments · May be fixed by #44
Open

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

burzum opened this issue Feb 26, 2020 · 4 comments · May be fixed by #44

Comments

@burzum
Copy link
Contributor

burzum commented Feb 26, 2020

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
Copy link
Member

jadb commented Feb 26, 2020

$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
Copy link
Contributor

Mr. Jad! 👋🏼

@burzum
Copy link
Contributor Author

burzum commented Mar 2, 2020

@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
Copy link
Contributor Author

burzum commented Mar 2, 2020

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']);
        }
    }

@jadb jadb linked a pull request Mar 2, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants