php-scoper icon indicating copy to clipboard operation
php-scoper copied to clipboard

How to exclude WordPress native classes by Regex?

Open nahid opened this issue 1 year ago • 4 comments

I want to exclude-classes for all the native WordPress classes by Regex? I also tried the given way, but it's not working

Tried these ways

exclude-classes => ['/^WP_/'],

// and

exclude-classes => ['WP_*'],

but these did not work properly. Please help me, how can I do this?

nahid avatar Nov 03 '24 04:11 nahid

I don't really understand what you mean by "nor work properly". Excluding classes does work for sure, there is plenty of tests, so if there is a peculiar broken case it should be possible to get a reproducer.

I recommend you to check https://github.com/humbug/php-scoper/blob/main/docs/further-reading.md#wordpress-support though

theofidry avatar Nov 03 '24 15:11 theofidry

Sorry for not being able to explain it clearly. The exclude-classes function works generally, but it's not working when I use regex. If I want to exclude all classes with the WP_ prefix, how can I achieve that using regex? @theofidry

nahid avatar Nov 04 '24 09:11 nahid

@theofidry You should really use the package sniccowp/php-scoper-wordpress-excludes for your WordPress excludes, as described in the documentation (though, well hidden under "further reading")

hirasso avatar Nov 04 '24 15:11 hirasso

@nahid I would need more details than "it is not working", because I simply do not understand what you mean by that. For instance if I have the following specs:

    'Declaration of an excluded class' => SpecWithConfig::create(
        excludeClasses: ['/^NewPhp.+$/'],
        expectedRecordedClasses: [
            ['NewPhp20Interface', 'Humbug\NewPhp20Interface'],
        ],
        spec: <<<'PHP'
            <?php

            interface NewPhp20Interface {}
            ----
            <?php

            namespace Humbug;

            interface NewPhp20Interface
            {
            }
            \class_alias('Humbug\NewPhp20Interface', 'NewPhp20Interface', \false);

            PHP,
    ),

    'Usage of an excluded class' => SpecWithConfig::create(
        excludeClasses: ['/^NewPhp.+$/'],
        spec: <<<'PHP'
            <?php

            class Foo implements NewPhp20Interface {}
            ----
            <?php

            namespace Humbug;

            class Foo implements \NewPhp20Interface
            {
            }

            PHP,
    ),

Both of those passes the tests.

theofidry avatar Nov 06 '24 09:11 theofidry