Avoid having properties with __get and no __set (PHP) | CAST Appmarq

Avoid having properties with __get and no __set (PHP)


Rule Definition
In PHP source code if there is a write action to a property that does not exist and there is no '__set'-method, the property will be created. The '__get' method will only be called if there is no property with the given name. So having properties without a __set method could produce unexpected values for the __get method of the same property.

Remediation
Modify the source code and add the '__set' method with an appropriate default. Also consider throwing an Exception that returns false.

Violation Code Sample
<?php
      $rec = new Rectangle(5, 5);
      echo $rec->area; //outputs 25

    ?>
Fixed Code Sample
<?php
      $rec->area = 10;
      echo $rec->area; //outputs 10
    ?>

Related Technologies

Health Factor

  Total Quality Index


Technical Criterion
Programming Practices - Unexpected Behavior

About CAST Appmarq

CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.

Benchmark Statistics

Global Compliance

96.64%

Total Violations
2,898
Total Opportunities
86,329
Average Violations / App.
57.96
The compliance score represents 1 minus the ratio between the number of times a rule has been violated compared to the number of opportunities in a set of applications that the rule could have been violated.

Industry Insights

IT & Business Consulting

90.87%

Telecommunications

97.37%

Insurance

89.26%