WordPress-Coding-Standards icon indicating copy to clipboard operation
WordPress-Coding-Standards copied to clipboard

Do not allow non class functions in a class file

Open kkmuffme opened this issue 6 years ago • 2 comments

Is your feature request related to a problem?

When a file contains a class, it should not allow any functions that are outside of this class. Since WPCS requires renaming files to include the class name, I think it would be consistent to not allow any functions that are not part of this class to be in this file.

Describe the solution you'd like

file: class-my-test.php

class my_test {

}

function do_something() {
    echo "hello";
}

Should get flagged for do_something, as its the file for my_test

kkmuffme avatar Oct 22 '19 14:10 kkmuffme

I like the idea of this sniff but would it be overbearing? Would it be best if it was only a warning level item?

While I prefer classes to have no side effects and also no functions outside the class scope I do see this pattern used (misused?) often and I think fixing it would be burden for some projects and quite a rework might need to happen for a lot of them to resolve this.

pattonwebz avatar Nov 07 '19 20:11 pattonwebz

Yes a warning level would be perfectly fine.

kkmuffme avatar Nov 08 '19 07:11 kkmuffme