Code Monkey home page Code Monkey logo

phpfmt8's People

Contributors

driade avatar nanch avatar nonines avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

nonines

phpfmt8's Issues

2 spaces are formatted after the callable type declaration

before format

<?php

class Server
{
    protected array $handlers = [];

    public function withHandler(callable | string $handler): static
    {
        $this->handlers[] = $this->createHandlerItem($handler);

        return $this;
    }
}

after format

<?php

class Server
{
    protected array $handlers = [];

    public function withHandler(callable  | string $handler): static
    {
        $this->handlers[] = $this->createHandlerItem($handler);

        return $this;
    }
}

2 spaces are formatted after the callable type declaration

Typed classes and properties not being saves properly

This is a code breaking bug.

Basically if you have something like this:


class Cache extends BaseConfig
{
    /**
     * --------------------------------------------------------------------------
     * Primary Handler
     * --------------------------------------------------------------------------
     *
     * The name of the preferred handler that should be used. If for some reason
     * it is not available, the $backupHandler will be used in its place.
     */
    public string $handler = 'file';

    /**
     * --------------------------------------------------------------------------
     * Backup Handler
     * --------------------------------------------------------------------------
     *
     * The name of the handler that will be used in case the first one is
     * unreachable. Often, 'file' is used here since the filesystem is
     * always available, though that's not always practical for the app.
     */
    public string $backupHandler = 'dummy';
}

In turn this will be changed into this:

class Cache extends BaseConfig
{
    /**
     * --------------------------------------------------------------------------
     * Primary Handler
     * --------------------------------------------------------------------------
     *
     * The name of the preferred handler that should be used. If for some reason
     * it is not available, the $backupHandler will be used in its place.
     */
    stringpublic $handler = 'file';

    /**
     * --------------------------------------------------------------------------
     * Backup Handler
     * --------------------------------------------------------------------------
     *
     * The name of the handler that will be used in case the first one is
     * unreachable. Often, 'file' is used here since the filesystem is
     * always available, though that's not always practical for the app.
     */
    stringpublic $backupHandler = 'dummy';
}

Enum format error.

before format

<?php

namespace App\Enums;

enum Mq: string
{
    case ORDER_PAYMENT_EXPIRED = 'order-payment-expired';

    public function getExchangeName(Mq $queue)
    {
        return 'ex-'.$queue->value;
    }
}

after format

<?php

namespace App\Enums;

enum Mq:string {
    case ORDER_PAYMENT_EXPIRED = 'order-payment-expired';

        public function getExchangeName(Mq $queue)
    {
            return 'ex-' . $queue->value;
        }
}

Problem with null as variable type

<?php

class B
{
    public int|string $a;
    public null|string $b;
    public ?|string $b;
    public null|\stdClass $b;
}

becomes

<?php

class B
{
    public int|string $a;
    public null|string $b;
    ?public |string $b;
    \stdClasspublic null| $b;
}

Unindenting the break; in the switch statement

I just installed the phpfmt on my latest update of the Sublime Text and I appreciate the developers for the work done thus far.

Some of my switch cases are like this:

switch ($action_to_perform) {
    case 'save_dataload':
        // Code for 'save_dataload' case
        break;
}

but I want to achieve this:

switch ($action_to_perform) {
case 'save_dataload':
    // Code for 'save_dataload' case
break;
}

I didn't find a settings to achieve this, so chat-GPT gave me a custom function to add to C:\Users\Michael\AppData\Roaming\Sublime Text\Packages\phpfmt

<?php

use Fmt\AdditionalPass;

class UnindentSwitchCaseBreak extends AdditionalPass
{
    public function candidate($source, $foundTokens)
    {
        // Check if the source code contains switch statements
        return (bool) strpos($source, 'switch');
    }

    public function format($source)
    {
        // Remove indentation from case and break statements
        $source = preg_replace('/\n(\s*)(case|break)/', "\n$1$2", $source);

        return $source;
    }
}

and I added UnindentSwitchCaseBreak to my [passes section], but it throws this error

[08-Aug-2023 11:00:31 Europe/Berlin] PHP Warning:  Undefined array key "_" in C:\Users\Michael\AppData\Roaming\Sublime Text\Packages\phpfmt\fmt.stub.php on line 3397
[08-Aug-2023 11:00:31 Europe/Berlin] PHP Notice:  fwrite(): Write of 56371 bytes failed with errno=32 Broken pipe in C:\Users\Michael\AppData\Roaming\Sublime Text\Packages\phpfmt\fmt.stub.php on line 3412

This is my settings file

{
    "excludes":
    [
        "PSR2ModifierVisibilityStaticOrder"
    ],
    "passes":
    [
        "UnindentSwitchCaseBreak",
    ],
    "php_bin": "C:/xampp/php/php.exe",

    "switch_case_space" : 1,
}

I am on a windows 11 machine running PHP 8.1.2

Pls help

AutoSemicolon bad inside match

match ($x) {
	$a => function () {
	};
};

$a = function () {
	function () {
	};
};

instead of

match ($x) {
	$a => function () {
	}
};

$a = function () {
	function () {
	}
};

Check what ExternalPass is for

There're references in the code to this "fmt-external.php" call, also seen in phpfmt-next, but it's utility seem to got lost.

optional arguments, there are space between question mark.

before format

<?php

class Server
{
    protected ServerRequestInterface $request;

    public function __construct(protected Merchant $merchant, ?ServerRequestInterface $request)
    {
        $this->request = $request ?? RequestUtil::createDefaultServerRequest();
    }
}

after format

<?php

class Server
{
    protected ServerRequestInterface $request;

    public function __construct(protected Merchant $merchant,  ? ServerRequestInterface $request)
    {
        $this->request = $request ?? RequestUtil::createDefaultServerRequest();
    }
}

optional arguments, there are space between question mark.

Space after "class" as a named parameter

This

return $action->handle(
    id: intval($id), class: TicketApiClassEnum::from($class)
);

becomes

return $action->handle(
    id: intval($id), class :TicketApiClassEnum::from($class)
);

Namespaces issues

use Vendor\Package\SomeNamespace\{
    SubnamespaceOne\ClassA,
    SubnamespaceOne\ClassB,
    SubnamespaceTwo\ClassY,
    ClassZ,
};

becomes

use Vendor\Package\SomeNamespace\SubnamespaceTwo\ClassY;

Named Arguments: There are no spaces between parameters and naming

before format

<?php

class Message
{
    protected $merchant;

    public function getMerchant(): Merchant
    {
        if (!$this->merchant) {
            $this->merchant = new Merchant(
                mchId: $this->config['mch_id'],
                serial: $this->config['serial']
            );
        }

        return $this->merchant;
    }
}

after format

<?php

class Message
{
    protected $merchant;

    public function getMerchant(): Merchant
    {
        if (!$this->merchant) {
            $this->merchant = new Merchant(
                mchId:$this->config['mch_id'],
                serial:$this->config['serial']
            );
        }

        return $this->merchant;
    }
}

auto format stops working when using the following sintax

Using typed functions where I want a fallback return or the params will have more than one type it does not work.

Example:

protected function allData(object|null $proposal): object|null
{
    return $proposal;
}

Basically it says that I have a syntax error and it does not run the auto format in this file.

My current local PHP version being used by FMT is the following:

php -v
PHP 8.2.2 (cli) (built: Feb  1 2023 08:33:04) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.2, Copyright (c) Zend Technologies

Issues with formatting of Arrays and Array Access for WordPress Coding Standards

Issues:

  1. As per WordPress Coding Standard, an array should be defined like this: (space at the beginning and closing of parentheses or square brackets)
    Screenshot 2023-05-30 at 10 09 59 AM
    Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#declaring-arrays

  2. As per WordPress Coding Standard, spaces while accessing array items will be like this:
    Screenshot 2023-05-30 at 10 06 16 AM
    Source: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#space-usage

Please, solve this issue by introducing some configurations regarding "[]" (Square brackets) so that WordPress Developers can write PHP and use the vscode-phpfmt with more ease.

Thank you.

Condition shorthand issue

Before

<?php

class Message
{
    public function test()
    {
        $campaign_data = ['enabled' = false];
        $campaign->status = $campaign_data['enabled'] ? Campaign::STATUS_ACTIVE : Campaign::STATUS_PAUSED;

        return $campaign;
    }
}

After

<?php

class Test
{
    public function test()
    {
        $campaign_data = ['enabled' = false];
        $campaign->status = $campaign_data['enabled']?Campaign::STATUS_ACTIVE: Campaign::STATUS_PAUSED;

        return $campaign;
    }
}

Configs:

{
	"version": 4,
	"php_bin": "/opt/homebrew/opt/php@7.4/bin/php",
	"format_on_save": false,
	"autocomplete": true,
	"autoimport": true,
	"disable_auto_align": true,
	"indent_with_space": true,
	"passes":
	[
		"StripExtraCommaInArray",
		"RemoveSemicolonAfterCurly",
		"NewLineBeforeReturn",
		"AddMissingParentheses",
		"ReindentSwitchBlocks",
		"OrderAndRemoveUseClauses",
		"DocBlockToComment",
		"PSR2EmptyFunction",
		"StripSpaceWithinControlStructures",
		"ShortArray",
		"AutoSemicolon",
		"OrderMethod", // 2023-06-05
		"OrganizeClass", // 2023-06-05
		"OrderMethodAndVisibility", // 2023-06-05
		"SortUseNameSpace", // 2023-06-05
		"SpaceBetweenMethods", // 2023-06-05
		"TrimSpaceBeforeSemicolon", // 2023-06-05
		"DoubleToSingleQuote"
	],
	"psr1": false,
	"psr2": true,
	"smart_linebreak_after_curly": true,
	"visibility_order": true,
	"yoda": false,
}

Invalid space between :: and (

Route::namespace ('App\Http\Controllers\Admin\Leads');

instead of

Route::namespace('App\Http\Controllers\Admin\Leads');

Format error with passes

When running cmd:

php fmt.stub.php --psr2 --indent_with_space=4 --passes=SpaceAfterExclamationMark --exclude=PSR2ModifierVisibilityStaticOrder

it prints Could not open input file: xxx/fmt-external.stub.php

Improve AutoSemicolon

Add semicolon in theses cases

$data['total'] = 1
/** comment **/

$data['total'] = 1
// comment

Infinite loop

When formatting

Collection::FOREACH;

We get into an infinite loop

Class Type declaration, T|null error

before format

<?php

class Application
{
    protected Qrcode|null $qrcode = null;
}

after format

<?php

class Application
{
    |protected Qrcodenull $qrcode = null;
}

PHP 5.6 support

According to php7mar these arre the only issues to fix if we want this tool to work with php 5.6

# nuance
#### /Users/davidfernandez/htdocs/phpfmt8/fmt.stub.php
* funcGetArg
 * Line 863: `				$params = array_slice(func_get_args(), 1);`
 * Line 2215: `			$args = func_get_args();`
 * Line 2240: `			$args = func_get_args();`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.