Daviid
Daviid

Reputation: 1594

Can't run phpcbf, tries to open in vscode

If I try to run phpcbf --version vscode opens and shows

The editor could not be opened due to an unexpected error: Unable to resolve resource phar://c:/PHP_Tools/phpcbf/src/Config.php

(Sometimes it's not Config.php but Runner.php)

then hangs, if I CTRL C I also get

The editor could not be opened due to an unexpected error: Unable to resolve resource phar://c:/Program%20Files/PHP_Tools/phpcbf/src/Config.php

(I moved the folder from Program Files, not sur why it's still there in the error)

Yesterday I found that .phar files were associated to VSCODE so I though maybe it was that, I deleted from regedit HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts and restarted, it worked but today it's failing again and the registry is still gone.

This happens with phpcbf and phpcs but not with phpmd and phpstan

C:\PHP_Tools
šŸ•™ 08:38:52 Ī» dir

        Directory: C:\PHP_Tools


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a---        16/04/2024     13:40        1460735 ļ…›  phpcbf
-a---        22/11/2024      8:28           1059 ī˜©  phpcbf.bat
-a---        16/04/2024     13:39        1460731 ļ…›  phpcs
-a---        22/11/2024      8:25             59 ī˜©  phpcs.bat
-a---        12/03/2024     10:57        3168622 ļ…›  phpmd
-a---        22/11/2024      8:25             59 ī˜©  phpmd.bat
-a---        12/03/2024     11:19       22528065 ļ…›  phpstan
-a---        22/11/2024      8:25             61 ī˜©  phpstan.bat
C:\PHP_Tools
šŸ•™ 08:39:17 Ī» cat .\phpcbf.bat
@echo off

REM Set a temporary file to store the output
set outputFile=%TEMP%\phpcbf_output.txt

REM Run phpcbf command and redirect output to the temporary file
C:\xampp7\php\php.exe "C:\PHP_Tools\phpcbf" %* > "%outputFile%"

REM Store the exit code of phpcbf
set exitCode=%ERRORLEVEL%

REM Print the exit code for debugging
echo Exit Code: %exitCode%

REM Check if the output contains the specific phrase
findstr /C:"PHPCBF FAILED TO FIX" "%outputFile%" > nul

REM Check the exit code of phpcbf and the presence of the phrase
IF %exitCode% EQU 0 (
    REM phpcbf encountered no fixable errors
    exit /b 0
) ELSE IF %exitCode% EQU 1 (
        REM phpcbf found error and fixed them or found unfixable errors
        IF ERRORLEVEL 1 (
        echo "The phrase was not found in the output"
        exit /b 0
    ) ELSE (
        echo "The phrase was found in the output"
        exit /b 1
    )
) ELSE (
        REM error code 2, fatal error, worng syntax?
    type "%outputFile%"
    exit /b 1
)
C:\PHP_Tools
šŸ•™ 08:39:21 Ī» cat .\phpcs.bat
@echo off

C:\xampp7\php\php.exe  "C:\PHP_Tools\phpcs" %*

phpcs and phpcbf were downloaded from PHPCSStandards/PHP_CodeSniffer

Upvotes: 0

Views: 29

Answers (1)

Daviid
Daviid

Reputation: 1594

I had to clone the git repository to run it as indicated in

git clone https://github.com/PHPCSStandards/PHP_CodeSniffer.git
cd PHP_CodeSniffer
php bin/phpcs -h
php bin/phpcbf -h

so I'd have the files separated and not in a phar.

Turns out that since in the file C:\PHP_Tools\PHP_CodeSniffer\Config.php handles --version like so

/**
     * Processes a long (--example) command-line argument.
     *
     * @param string $arg The command line argument.
     * @param int    $pos The position of the argument on the command line.
     *
     * @return void
     * @throws \PHP_CodeSniffer\Exceptions\DeepExitException
     */
    public function processLongArgument($arg, $pos)
    {
        switch ($arg) {
        case 'help':
            ob_start();
            $this->printUsage();
            $output = ob_get_contents();
            ob_end_clean();
            throw new DeepExitException($output, 0);
        case 'version':
            $output  = 'PHP_CodeSniffer version '.self::VERSION.' ('.self::STABILITY.') ';
            $output .= 'by Squiz and PHPCSStandards'.PHP_EOL;
            throw new DeepExitException($output, 0);
        case 'colors':
            if (isset(self::$overriddenDefaults['colors']) === true) {
                break;
            }

            $this->colors = true;
            self::$overriddenDefaults['colors'] = true;
            break;
        case 'no-colors':
            if (isset(self::$overriddenDefaults['colors']) === true) {
                break;
            }

            $this->colors = false;
            self::$overriddenDefaults['colors'] = true;
            break;
        case 'cache':
            if (isset(self::$overriddenDefaults['cache']) === true) {
                break;
            }

            if (defined('PHP_CODESNIFFER_IN_TESTS') === false) {
                $this->cache = true;
                self::$overriddenDefaults['cache'] = true;
            }
            break;
        case 'no-cache':
            if (isset(self::$overriddenDefaults['cache']) === true) {
                break;
            }

            $this->cache = false;
            self::$overriddenDefaults['cache'] = true;
            break;


[....]

and my debugger was running with breakpoints on Exceptions, it was catching these DeepExitException and trying to open the phar://c:/PHP_Tools/phpcbf/src/Config.php

The fix when I restarted was probably because I hadn't started the debugger yet.

Upvotes: 1

Related Questions