Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

[Bug] DevTools does not detect use of CrudTrait if not the first entry in use statement #1196

Closed btrumsey closed 1 month ago

btrumsey commented 1 month ago

Bug report

What I did

Viewing my models in DevTools incorrectly flags models as not using CrudTrait.

What I expected to happen

Any model using CrudTrait should be checked in the Model list view.

What happened

Any model that does not specify CrudTrait as the first entry after the use statement will show as not using CrudTrait.

What I've already tried to fix it

The error is in the getHasCrudTraitAttribute function in Model.php. The check is being done with a contains call, which will require an exact match:

// cover the case where the trait has its namespace imported
if ($content->contains('use Backpack\CRUD\app\Models\Traits\CrudTrait') && $content->contains('use CrudTrait')) {
    return true;
}

Updating this to use a regular expression will properly identify the usage of CrudTrait:

// cover the case where the trait has its namespace imported
if ($content->contains('use Backpack\CRUD\app\Models\Traits\CrudTrait') && Str::of($content)->match('/use\s+.*\bCrudTrait\b/')) {
    return true;
}

Is it a bug in the latest version of Backpack?

Yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
8.2.24

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, calendar, ctype, curl, dom, mbstring, FFI, fileinfo, ftp, gd, gettext, iconv, imagick, exif, mysqli, pdo_mysql, pdo_sqlite, Phar, posix, readline, shmop, SimpleXML, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tokenizer, xmlreader, xmlwriter, xsl, zip, Zend OPcache, xdebug

### LARAVEL VERSION:
11.28.0.0

### BACKPACK PACKAGE VERSIONS:
backpack/activity-log: 2.0.5
backpack/backupmanager: v5.0.5
backpack/basset: 1.3.6
backpack/crud: 6.7.41
backpack/devtools: 3.1.6
backpack/editable-columns: 3.0.10
backpack/generators: v4.0.7
backpack/logmanager: v5.0.2
backpack/medialibrary-uploaders: 1.2.1
backpack/menucrud: v4.0.2
backpack/pagemanager: 3.3.2
backpack/permissionmanager: 7.2.1
backpack/pro: 2.2.21
backpack/settings: 3.1.1
backpack/theme-tabler: 1.2.13
pxpm commented 1 month ago

Hey @btrumsey thanks for the heads up, and for providing a possible solution.

I've just released backpack/devtools 3.1.7 with the fix. I decided to not use regex as you suggested because it could also leave other use cases missing, like for example when extending models:

class ModelB 
{
    use CrudTrait;
}

class ModelA extends ModelB
{
}

I've changed the script to use \Reflection instead so all use cases are covered.

Please allow a few moments for our private repository to pick the released changes and you can get the updated version with a composer update backpack/devtools 👍

Please let us know if you are still experiencing issues 🙏

Cheers