BrianHenryIE / strauss

Prefix PHP namespaces and classnames to allow multiple versions of libraries to exist without conflict.
https://brianhenryie.github.io/strauss/
MIT License
142 stars 23 forks source link

Class whose function returns an instance of itself calling the unprefixed class name in its return statement #95

Closed foinf closed 4 months ago

foinf commented 6 months ago

For example (scroll to the return statement at the end of the class definition):

/**
 * @private
 *
 * @license Apache-2.0
 * Modified by __root__ on 18-April-2024 using {@see https://github.com/BrianHenryIE/strauss}.
 */
class Electrickiwi_BB_Theme_Less_Tree_Url extends Electrickiwi_BB_Theme_Less_Tree {

    public $attrs;
    public $value;
    public $currentFileInfo;
    public $isEvald;
    public $type = 'Url';

    /**
     * @param Electrickiwi_BB_Theme_Less_Tree_Variable|Electrickiwi_BB_Theme_Less_Tree_Quoted|Electrickiwi_BB_Theme_Less_Tree_Anonymous $value
     * @param array|null $currentFileInfo
     * @param bool|null $isEvald
     */
    public function __construct( $value, $currentFileInfo = null, $isEvald = null ) {
        $this->value = $value;
        $this->currentFileInfo = $currentFileInfo;
        $this->isEvald = $isEvald;
    }

    public function accept( $visitor ) {
        $this->value = $visitor->visitObj( $this->value );
    }

    /**
     * @see Electrickiwi_BB_Theme_Less_Tree::genCSS
     */
    public function genCSS( $output ) {
        $output->add( 'url(' );
        $this->value->genCSS( $output );
        $output->add( ')' );
    }

    /**
     * @param Electrickiwi_BB_Theme_Less_Environment $ctx
     */
    public function compile( $ctx ) {
        $val = $this->value->compile( $ctx );

        if ( !$this->isEvald ) {
            // Add the base path if the URL is relative
            if ( Electrickiwi_BB_Theme_Less_Parser::$options['relativeUrls']
                && $this->currentFileInfo
                && is_string( $val->value )
                && Electrickiwi_BB_Theme_Less_Environment::isPathRelative( $val->value )
            ) {
                $rootpath = $this->currentFileInfo['uri_root'];
                if ( !$val->quote ) {
                    $rootpath = preg_replace( '/[\(\)\'"\s]/', '\\$1', $rootpath );
                }
                $val->value = $rootpath . $val->value;
            }

            $val->value = Electrickiwi_BB_Theme_Less_Environment::normalizePath( $val->value );
        }

        // Add cache buster if enabled
        if ( Electrickiwi_BB_Theme_Less_Parser::$options['urlArgs'] ) {
            if ( !preg_match( '/^\s*data:/', $val->value ) ) {
                $delimiter = strpos( $val->value, '?' ) === false ? '?' : '&';
                $urlArgs = $delimiter . Electrickiwi_BB_Theme_Less_Parser::$options['urlArgs'];
                $hash_pos = strpos( $val->value, '#' );
                if ( $hash_pos !== false ) {
                    $val->value = substr_replace( $val->value, $urlArgs, $hash_pos, 0 );
                } else {
                    $val->value .= $urlArgs;
                }
            }
        }

        return new Less_Tree_URL( $val, $this->currentFileInfo, true );
    }

}

In the last line, the compile function is (I assume) returning the instance of the class but this causes a fatal error because Class 'Less_Tree_URL' is not found:

Fatal error: Uncaught Error: Class 'Less_Tree_URL' not found in /<path>/staging_html/wp-content/themes/<theme>/vendor-prefixed/wikimedia/less.php/lib/Less/Tree/Url.php:77

I used strauss.phar and the only config I supplied was the following:

"extra": {
        "strauss": {
            "target_directory": "vendor-prefixed",
            "namespace_prefix": "Electrickiwi\\BB_Theme\\",
            "classmap_prefix": "Electrickiwi_BB_Theme_"
        }
    }

Another library Timber/Timber which was in the same vendor directory works fine. Is there anything I am missing or any step I didn't take?

Thanks!

BrianHenryIE commented 6 months ago

That looks like a bug, yes.

But also, wikimedia/less.php's code on GitHub is return new self( $val, $this->currentFileInfo, true ); which should work ok, so are you somehow using an old version of the library? If you update you can get past this bug without waiting on me.

foinf commented 6 months ago

That looks like a bug, yes.

But also, wikimedia/less.php's code on GitHub is return new self( $val, $this->currentFileInfo, true ); which should work ok, so are you somehow using an old version of the library? If you update you can get past this bug without waiting on me.

Hi, thanks for response! Yes, I did figure out I was using an older version (as it was a dependency of an outdated package) however, I have run into yet another problem which I can confirm is caused by strauss.

If I use the 4.2.1 version of less.php, it generates the styles just fine.

But if I run strauss on it and then use the prefixed package, this is what happens:

Warning: foreach() argument must be of type array|object, null given in <path>\mamp_docs\less_test\vendor-prefixed\wikimedia\less.php\lib\Less\Tree\Ruleset.php on line 426

The above error multiple times with the result being:

{display:none !important}/* :root { --swiper-navigation-color: #c09e79; --a:blue; } */{box-sizing:border-box}{text-decoration:none}{margin:0}{text-decoration:none}{display:grid;grid-template-rows:1fr auto}{height:100%}{min-height:100%}{width:100%;/*overflow-x: hidden;*/}{text-transform:lowercase}

As you can see, the output has no selectors in it, just the rules. This is happening on the dev server (unix) I am working on as well as a minimalist example I created on MAMP on windows to test.

BrianHenryIE commented 6 months ago

The real problem initially was that the class name is Less_Tree_Url and the return type was new Less_Tree_URL, so that's not a bug here but a bug in the library which has been fixed.

I can't quite see if the second issue is related to this library. That error message is saying $this->paths is null (public $paths). Can you give me step by step instructions and output of the library working before running Strauss and then the buggy behaviour after?

foinf commented 6 months ago

Hi,

Yes, I can share the results with you when I am on my computer. But, I think you're correct that it's not really a strauss issue because if that were true, then the timber/timber package as well as the scssphp/scssphp package (switched to the latter after wasting a lot of time on the less.php one) should have caused errors as well but both these packages work fine.

So, if you think that the bug label is incorrect, this thread can be moved/closed/relabeled as you see fit.

However, I will share the results of less.php output for both prefixed as well as unprefixed files ASAP.

Thanks!