Comments (6)
I'm the author of the PR so I'm biased but 100% agree with @derrabus
For instance getUser(): ?User naturally reads as "this method will return a user or no user"
getUser(): User|null
reads the same.
from coding-standard.
Yes. int|null
is better than int|string
because as a set it has less capacity (null
is just one distinct value) and often has the same semantics of "no value". The more specific the type is, the better.
The usage of NULL often naturally reflects the nature of the model being programmed. For instance getUser(): ?User
naturally reads as "this method will return a user or no user". At the same time, the meaning of setParameter(int|string $parameter, ...)
is not that clear.
from coding-standard.
The type declarations ?SomeType
and SomeType|null
are 100% equivalent and this rule enforces the latter. If we have multiple ways to express the same thing, it is perfectly reasonable to enforce one of the ways via a CS rule.
union types are often poor
A nullable type is a union. Your arguments apply to nullable types as they apply to any other union and I understand that avoiding unions is probably a good idea. However, I fail to see how this is relevant for our coding standard.
I don't think we should roll back.
from coding-standard.
Language shapes the way we think. I do not want the language (the coding standard) to force me into thinking in the broad categories of union types, I want to be able to use more expressive and specific nullable types, where necessary.
In my opinion, union types in PHP are a smell, and I do not want this smell to be enforced. Note, a smell doesn't mean that something is definitely wrong but it's often a sign that something is potentially wrong. As a reader of the code, I want to be able to spot a union without applying extra effort.
If we have multiple ways to express the same thing, it is perfectly reasonable to enforce one of the ways via a CS rule.
Following this logic, we should enforce the alphabetical order of class member declaration.
from coding-standard.
@morozov are you saying int|string
and int|null
are not equally bad, and that you want to spot the former more easily? In yes, can you please elaborate on why they are not equally bad?
from coding-standard.
I agree that I don't feel the same amount of disgust when I see ?User
than when I see User|Car
, and that maybe this case deserves a special treatment because it's not the same level of bad, so I'd lean more on the side of ?
getUser(): User|null reads the same.
To me it reads like "this method will return a user or null" … in case of failure?
from coding-standard.
Related Issues (20)
- Use stable release of slevomat/coding-standard HOT 4
- PSR-12 integration HOT 2
- A phpDoc template closing tag is mistakenly considered belonging to the following function
- False positive using intersection types in type annotation HOT 4
- Add SlevomatCodingStandard.Functions.StrictCall HOT 3
- Forbid more functions
- The php72 test-report job is always successful HOT 5
- doctrine/coding-standard 8.0.0 requires dealerdirect/phpcodesniffer-composer-installer ^0.6.2 HOT 1
- phpcs: ERROR: Referenced sniff "SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace" does not exist HOT 1
- duplicate rules for visibility of class constants
- class and properties with no spaces between them seem to be allowed
- Ignore some case for EarlyExit rule HOT 1
- Consider dropping auto fix of `SlevomatCodingStandard.Classes.ClassConstantVisibility` HOT 5
- suggestion: a good readme HOT 2
- Support for PHPCS ^4.0 HOT 2
- Add WordpressArray sniff HOT 4
- New version HOT 5
- Duplicate `RequireOneLineDocComment` in ruleset HOT 1
- Switch to phpcsstandards/codesniffer HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coding-standard.