Saturday, 15 May 2010

php - Avoiding $onlyActive boolean parameter -



php - Avoiding $onlyActive boolean parameter -

i started using phpmd observe bad coding practices , prepare them. project uses laravel 4 php framework , implement repository pattern.

so have class called eloquentproductrepository interacts product tables in database. methods all() method have boolean parameter called $onlyactive. when true fetches active products, otherwise returned.

phpmd tells me boolean parameters sign of violation of single responsibility pattern. did reading , agree booleans should avoided. question how should refactor regards maintainability, readability , extendibility?

the method rather simple , looks follows

/** * fetches products * * @param boolean $onlyactive flag returning active products * @return collection */ public function all($onlyactive = true) { if ($onlyactive) { homecoming $this->model->where('active', true)->get(); } homecoming $this->model->all(); }

i see 2 options. 1 using $options array instead key 'include_inactive'. other alternative creating 2 methods. all() , allwithinactive(). have 3 methods using $onlyactive boolean lastly alternative add together 3 methods class might create class rather big on methods. (phpmd prefers classes have no more 10 public methods)

you have 2 different functions - how it

public function getall() { homecoming $this->model->all(); } public function getonlyactive() { homecoming $this->model->where('active', true)->get(); }

note function names - getall() - records, no exceptions. getonlyactive() - active records - there no "all" in function name, because doesnt "all".

php methods laravel-4 boolean

No comments:

Post a Comment