Two properties, one name
Two properties, one name

Find private properties in my PHP

It is always good to use the lowest visibility possible for a class member, and only relax the constraint when it is really needed. This is probably a old adage, shared with other programming languages, and one that I’m trying to apply more and more.

Public properties will act as globals : suddenly, across the code and for unexpected reasons, the property is changed. The owning object has no warning and not a change to adapt it’s consistence, and will probably end up being inconsistant. Then it will need more validation whenever the object is being used.

So, the sane move is to make properties private, use methods to access them. With some discipline, it is possible in PHP. But to check one’s code, you needs tools that will take the source code as an whole, and pore over it all before confirming that a public property may be set to private.

Now, such a search is actually an good search for static analysis, so I wrote a rule for that with Exakat. The engine checks for protected and private properties.

Properties sharing name across classes

Protected properties usage are searched in the extended classes, and never in the parents. This is very straight forward. Public properties are search outside the definition class, in the rest of the project. This is fairly larger, but it raises an interesting question : how are related properties that bear the same name but are in different classes ?

Let’s imagine an ‘$birthDate’ property. It may have several related methods, just like this :

<?php
class Person {
   private $birthDate;

// Setting the date
   public setBirth(Datetime $date) { $this→birthDate = $date; }

// Getting the age at a point in time or Now() by default
   public getAge($date = null) {
      if ($date === null) { $date = time(); }
      return $this→birthDate - $date;
   }
}
?>

This notion of birthDate is great for a Person. It could be applied to an animal or a plant, very straightforwardly, though chichken could be ‘hatched’. As a conception, the first idea is going to build this as an abstract class LivingThing or an interface LivingThingInterface. Then, ‘Building’ and ‘Software’ may also extend this class, though there is now a need to adapt the vocabulary (software are published, buidlings are erected or built, not born). On the other hand, another approach is to export this as a Trait, with the same vocabulary problem.

Finally, back to the first problem. If two different classes has a property with the same name, does it means that the notion is the same in those two classes ? In that case, the code could be turned into a trait or an abstract class. Otherwise, those properties may simply carry an ambiguous name. The same word may have different definition, like ‘buffalo’ or ‘port’, as in “In port, the portly porter ported the port, through the port port”.  (I like better the ‘buffalo’, and ‘port’ is just more common).

Here is a quick check on a real code source. Composer’s source code has 309 classes, 556 distinct properties and 82 properties that are defined in several classes. 9 properties are public, 183 are private, 364 are protected, none are redefined : quite a good job at keeping the classes closed. Among those properties, there are $io, available in 27 different classes, $config in 19 and $process in 14. $composer is also defined in 7 different classes.

Test your own code

If you want to check if some properties may be made private, get the upcoming exakat 0.4.6 (Coming up next Monday), along with several other goodies and upgrade.