WordPress / WordPress/WordPress-Coding-Standards

Do not allow non class functions in a class file

Open
#1,816 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Needs Make post
Dominant language
PHP
Stars
2.8k
Forks
521
Avg merge
5d 20h
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Search the PHP_CodeSniffer rules for the check that enforces class-file naming and trace how class and function declarations are inspected. Add coverage for a file containing a class plus an unrelated global function, with completion defined as that function being flagged while class members remain allowed.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.