googleapis / googleapis/google-api-php-client-services

Proper PHPDoc for nullable class properties and method returns

Aperta
#604 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
type: feature request
Lingua principale
PHP
Stelle
1.3k
Fork
320
Merge medio
2h 32m
PR unite (30g)
193

Descrizione

Related / follow-up of https://github.com/googleapis/google-api-php-client-services/issues/172
**Is your feature request related to a problem? Please describe.**
Currently some classes have properties that

- can be NULL
- have PHPDoc which suggests it can not be NULL

For example:
https://github.com/googleapis/google-api-php-client-services/blob/dbd60410b289374a44fe91f3556d463b55c10246/src/Calendar/EventDateTime.php#L23-L25

And https://github.com/googleapis/google-api-php-client-services/blob/dbd60410b289374a44fe91f3556d463b55c10246/src/Calendar/EventDateTime.php#L42-L48

When using static analysis using (for example) PHPStan, on code like this:

```php
//All-day event has the date property set
if (is_string( $calendarEvent->getStart()->date)) {
//Process all-day event
}
} elseif (is_string( $calendarEvent->getStart()->dateTime)) {
//event is not all-day-even
}
```
This will result in an error like:

```php
Elseif branch is unreachable because previous condition is always true.
```

**Describe the solution you'd like**
It would be better to use PHPDoc to indicate that the property can be null, for example

```php
/**
* @var string|null
*/
public $date;

/**
* @return string|null
*/
public function getDate()
{
return $this->date;
}
```
**Describe alternatives you've considered**
For PHP versions that support typed properties and return types, the PHPDoc can be omitted:

```php
public ?string $date = null;
public function getDate() : ?string
{
return $this->date;
}
```

But this library supports PHP `>=5.6`, so this is probably not an option yet: https://github.com/googleapis/google-api-php-client-services/blob/main/composer.json#L9

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start with src/Calendar/EventDateTime.php at the linked property and getter examples, then review the related issue 172 and the PHP version constraint in composer.json. Check how nullable properties and returns are documented across the affected classes, and use PHPStan or equivalent static analysis to verify that valid nullable branches are no longer reported as unreachable.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
php
Ambito
api, developer-experience
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.