Code Monkey home page Code Monkey logo

Comments (14)

sankaest avatar sankaest commented on September 22, 2024 3

In case somebody is searching for those issues and try to fix by themself, below is regex, what worked about 95% of situations (eg, will not match closure function in method).

OneLiners concatenated together by OR (|):
([a-zA-Z0-9$]|[ ])+->\w*\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(|\((new .*)\)\)->\w*\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(|([a-zA-Z0-9])+\:\:\w*\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(|[a-zA-Z0-9$]+->\w*\((([^\(\)]+|))\)\n[ ]*->[a-zA-Z0-9$]+\(|\((new .*)\)\)->\w*\((([^\(\)]+|))\)\n[ ]*->[a-zA-Z0-9$]+\(|[a-zA-Z0-9$]+\n[ ]*->[a-zA-Z0-9$]+\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(|\((new .*)\)\)\n[ ]*->[a-zA-Z0-9$]+\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(

Explanation

1.1 Search for single line multiple chain, no rounded brackets [^\(\)] in selection.
Pattern: $object->method1($attributes)->method2(
Regex: ([a-zA-Z0-9$]|[ ])+->\w*\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(

1.2 The same as above, but for new objects.
Pattern: (new Object())->method1($attributes)->method2(
Regex: \((new .*)\)\)->\w*\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(

1.3 The same as 1.1, but for static methods
Pattern: Object::method1($attributes)->method2(
Regex: ([a-zA-Z0-9])+\:\:\w*\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(

2.1 Search for multiple line multiple chain (first method on the same line as object - chaining).
Pattern:

$object->method1($attributes)
    ->method2(

Regex: [a-zA-Z0-9$]+->\w*\((([^\(\)]+|))\)\n[ ]*->[a-zA-Z0-9$]+\(

2.2 The same as above, but for new objects.
Pattern:

(new Object())->method1($attributes)
    ->method2(

Regex: \((new .*)\)\)->\w*\((([^\(\)]+|))\)\n[ ]*->[a-zA-Z0-9$]+\(

3.1 Search for multiple line multiple chain (first method on the next line second method on the same line as first method - chaining).
Pattern:

$object
    ->method1($attributes)->method2(

Regex: [a-zA-Z0-9$]+\n[ ]*->[a-zA-Z0-9$]+\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(

3.2 The same as above, but for new objects.
Pattern:

(new Object())
    ->method1($attributes)->method2(

Regex: \((new .*)\)\)\n[ ]*->[a-zA-Z0-9$]+\((([^\(\)]+|))\)->[a-zA-Z0-9$]+\(

Use with care :)

from docs.

sankaest avatar sankaest commented on September 22, 2024 2

More examples (I had during code review), are they correct?

one argument (single method) with concatenation:

    $this->assertGreaterThan($customerQuery->count() . " " . print_r($orders, true));

one argument (chain calls):

    $this->assertCount(
        $query
            ->createCommand()
            ->getRawSql() 
    );

multiple arguments (single/chained), with concatenation:

    $this->assertCount(
        1,
        $orders,
        $query
            ->createCommand()
            ->getRawSql() . 
        print_r($orders, true)
    );

chaining in if() with multiple conditions:

    if (
        $db
            ->getSchema()
            ->getTableSchema($tablename, true) === null
    ) {
        // code
    }

property after method:

    $this->assertFalse(
        $boolARQuery
            ->where(['bool_col' => false])
            ->one()->bool_col
    );

from docs.

samdark avatar samdark commented on September 22, 2024 1

I usually do it as:

$object
    ->method1()
    ->method2();

from docs.

samdark avatar samdark commented on September 22, 2024 1

We may have it as a soft-rule with should instead of must.

from docs.

samdark avatar samdark commented on September 22, 2024 1

Yes, it is.

from docs.

arogachev avatar arogachev commented on September 22, 2024

Already checked and seems it can't be automated with StyleCI.

from docs.

tomaszkane avatar tomaszkane commented on September 22, 2024

I usually do it as:

$object
    ->method1()
    ->method2();

Me too, and also when chain is long sometime writes it as:

$object
    ->methot1()
    ->methot2()
    ->methot3()
    ->methot4()
    ->methot5()
;

to easy move/remove any method.

from docs.

vjik avatar vjik commented on September 22, 2024

This rule is overhead for me. Don't want to significantly expand the PSR-12.

Although, I prefer @samdark variant:

$object
    ->method1()
    ->method2();

from docs.

sankaest avatar sankaest commented on September 22, 2024

What about such inliners?

in array (from: yii3-app/config/routes.php):

return [
    Route::get('/')->action([SiteController::class, 'index'])->name('home'),
];

as variable (yii3-app/tests/Unit/HelloCest.php):
$params = $this->getConfig()->get('params');

as return (yii3-app/tests/Support/_generated/UnitTesterActions.php):
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertEmpty', func_get_args()));

It will be more clear if it would be stated here as examples of different usage cases.

from docs.

sankaest avatar sankaest commented on September 22, 2024

Is this correct?

return [
    Route::get('/')
        ->action([SiteController::class, 'index'])
        ->name('home'),
];
$params = $this
    ->getConfig()
    ->get('params');
 return $this
    ->getScenario()
    ->runStep(new \Codeception\Step\Action('assertEmpty', func_get_args()));

from docs.

samdark avatar samdark commented on September 22, 2024

@sankaest merged all PRs opened so far. Thanks!

from docs.

xepozz avatar xepozz commented on September 22, 2024

Use with care! and save your time!

No one will use it. It can be implemented as a plugin to such tools as php-cs-fixer, rector, prettier or others.

from docs.

sankaest avatar sankaest commented on September 22, 2024

Well, if somebody will make a plugin I will be happy. At least until now, no changes were made and as @arogachev was pointed out: "Already checked and seems it can't be automated with StyleCI." And within two month, nobody made any changes according to this issue. But if somebody will make a plugin, let me know, so I could be happy. :) @xepozz can you make it?

from docs.

xepozz avatar xepozz commented on September 22, 2024

Well, if somebody will make a plugin I will be happy. At least until now, no changes were made and as @arogachev was pointed out: "Already checked and seems it can't be automated with StyleCI." And within two month, nobody made any changes according to this issue. But if somebody will make a plugin, let me know, so I could be happy. :) @xepozz can you make it?

I don't think so :)

StyleCI should be omitted as for me.

So with php-cs-fixer you can create your own rule. It's simple.
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/custom_rules.rst

from docs.

Related Issues (20)

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.