rspec/rules/S1784/php/rule.adoc

49 lines
1.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
Explicitly declaring method visibility provides clarity and improves code readability. This leads to:
2020-06-30 12:47:33 +02:00
* Simplifying access to and use of the methods for other developers, enhancing the maintainability of the codebase.
* Promoting the principle of encapsulation, allowing you to control the accessibility of methods and prevent unintended access or modifications from external code.
2020-06-30 12:47:33 +02:00
By explicitly stating the visibility, you establish a clear contract for how the methods should be interacted with, making it easier to understand and reason about the behavior of the code.
Available access modifiers are:
* ``++private++`` - access allowed only within the same class
* ``++protected++`` - access allowed to the class and its child classes
* ``++public++`` - unfettered access by all (default)
== How to fix it
=== Code examples
==== Noncompliant code example
[source,php,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
function foo(){...}
----
==== Compliant solution
2020-06-30 12:47:33 +02:00
[source,php,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
private function foo(){...}
2020-06-30 12:47:33 +02:00
----
== Resources
=== Documentation
* https://www.php.net/manual/en/language.oop5.visibility.php[PHP Manual - Visibility]
* https://www.w3schools.com/php/php_oop_access_modifiers.asp[W3Schools - Access Modifiers]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
endif::env-github,rspecator-view[]