1.2.1010. Property Used In One Method Only¶
Properties should be used in several methods. When a property is used in only one method, this should have be of another shape.
Properties used in one method only may be used several times, and read only. This may be a class constant. Such properties are meant to be overwritten by an extending class, and that’s possible with class constants.
Properties that read and written may be converted into a variable, static to the method. This way, they are kept close to the method, and do not pollute the object’s properties. This analysis consider that using the current object with a cast or with the get_object_vars() function is also a usage, and skip those properties.
Note : properties used only once are not returned by this analysis. They are omitted, and are available in the analysis `Used Once Property`_.
<?php
class foo {
private $once = 1;
const ONCE = 1;
private $counter = 0;
function bar() {
// $this->once is never used anywhere else.
someFunction($this->once);
someFunction(self::ONCE); // Make clear that it is a
}
function bar2() {
static $localCounter = 0;
$this->counter++;
// $this->once is only used here, for distinguising calls to someFunction2
if ($this->counter > 10) { // $this->counter is used only in bar2, but it may be used several times
return false;
}
someFunction2($this->counter);
// $localCounter keeps track for all the calls
if ($localCounter > 10) {
return false;
}
someFunction2($localCounter);
}
}
?>
1.2.1010.1. Connex PHP features¶
1.2.1010.1.1. Suggestions¶
Drop the property, and inline the value
Drop the property, and make the property a local variable
Use the property in another method
1.2.1010.1.2. Specs¶
Short name |
Classes/PropertyUsedInOneMethodOnly |
Rulesets |
|
Exakat since |
0.10.3 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
Very high |
Examples |
|
Available in |