Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPstan level 2 #498

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"require-dev" : {
"friendsofphp/php-cs-fixer": "~2.16.1",
"phpunit/phpunit" : "^7.5 || ^8.5 || ^9.0",
"phpstan/phpstan": "^0.12"
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12.6"
},
"suggest" : {
"hoa/bench" : "If you would like to run the benchmark scripts"
Expand Down
6 changes: 3 additions & 3 deletions lib/BirthdayCalendarGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BirthdayCalendarGenerator
/**
* Input objects.
*
* @var array
* @var Component\VCard[]
*/
protected $objects = [];

Expand All @@ -39,7 +39,7 @@ class BirthdayCalendarGenerator
* Check the setTimeRange and setObjects methods for details about the
* arguments.
*
* @param mixed $objects
* @param string[]|string|Component\VCard[]|Component\VCard|null $objects
*/
public function __construct($objects = null)
{
Expand All @@ -54,7 +54,7 @@ public function __construct($objects = null)
* You must either supply a vCard as a string or as a Component/VCard object.
* It's also possible to supply an array of strings or objects.
*
* @param mixed $objects
* @param string[]|string|Component\VCard[]|Component\VCard $objects
*/
public function setObjects($objects)
{
Expand Down
35 changes: 13 additions & 22 deletions lib/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Sabre\VObject;

use
InvalidArgumentException;
use InvalidArgumentException;

/**
* This is the CLI interface for sabre-vobject.
Expand Down Expand Up @@ -314,17 +313,14 @@ protected function showHelp()
*
* @return int
*/
protected function validate(Component $vObj)
protected function validate(Document $vObj)
{
$returnCode = 0;

switch ($vObj->name) {
case 'VCALENDAR':
$this->log('iCalendar: '.(string) $vObj->VERSION);
break;
case 'VCARD':
$this->log('vCard: '.(string) $vObj->VERSION);
break;
if ($vObj instanceof Component\VCalendar) {
$this->log('iCalendar: '.(string) $vObj->VERSION);
} elseif ($vObj instanceof Component\VCard) {
$this->log('vCard: '.(string) $vObj->VERSION);
}

$warnings = $vObj->validate();
Expand Down Expand Up @@ -354,17 +350,14 @@ protected function validate(Component $vObj)
*
* @return int
*/
protected function repair(Component $vObj)
protected function repair(Document $vObj)
{
$returnCode = 0;

switch ($vObj->name) {
case 'VCALENDAR':
$this->log('iCalendar: '.(string) $vObj->VERSION);
break;
case 'VCARD':
$this->log('vCard: '.(string) $vObj->VERSION);
break;
if ($vObj instanceof Component\VCalendar) {
$this->log('iCalendar: '.(string) $vObj->VERSION);
} elseif ($vObj instanceof Component\VCard) {
$this->log('vCard: '.(string) $vObj->VERSION);
}

$warnings = $vObj->validate(Node::REPAIR);
Expand Down Expand Up @@ -393,11 +386,11 @@ protected function repair(Component $vObj)
/**
* Converts a vObject file to a new format.
*
* @param Component $vObj
* @param Component\VCard|Component\VCalendar $vObj
*
* @return int
*/
protected function convert($vObj)
protected function convert(Document $vObj)
{
$json = false;
$convertVersion = null;
Expand Down Expand Up @@ -458,8 +451,6 @@ protected function convert($vObj)
* Colorizes a file.
*
* @param Component $vObj
*
* @return int
*/
protected function color($vObj)
{
Expand Down
18 changes: 4 additions & 14 deletions lib/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
*/
class Component extends Node
{
/**
* Component name.
*
* This will contain a string such as VEVENT, VTODO, VCALENDAR, VCARD.
*
* @var string
*/
public $name;

/**
* A list of properties and/or sub-components.
*
Expand Down Expand Up @@ -169,7 +160,7 @@ public function remove($item)
* Returns a flat list of all the properties and components in this
* component.
*
* @return array
* @return (Property|Component\VAlarm|Component\VAvailability|Component\VEvent|Component\VFreeBusy|Component\VJournal|Component\VTimeZone|Component\VTodo)[]
*/
public function children()
{
Expand Down Expand Up @@ -214,7 +205,7 @@ public function getComponents()
*
* @param string $name
*
* @return array
* @return Node[]
*/
public function select($name)
{
Expand Down Expand Up @@ -431,7 +422,7 @@ protected function getDefaults()
*
* @param string $name
*
* @return Property
* @return Property|Component|null
*/
public function __get($name)
{
Expand All @@ -441,10 +432,9 @@ public function __get($name)

$matches = $this->select($name);
if (0 === count($matches)) {
return;
return null;
} else {
$firstMatch = current($matches);
/* @var $firstMatch Property */
$firstMatch->setIterator(new ElementList(array_values($matches)));

return $firstMatch;
Expand Down
10 changes: 5 additions & 5 deletions lib/Component/VAlarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getEffectiveTriggerTime()

$parentComponent = $this->parent;
if ('START' === $related) {
if ('VTODO' === $parentComponent->name) {
if ($parentComponent instanceof VTodo) {
$propName = 'DUE';
} else {
$propName = 'DTSTART';
Expand All @@ -43,9 +43,9 @@ public function getEffectiveTriggerTime()
$effectiveTrigger = $parentComponent->$propName->getDateTime();
$effectiveTrigger = $effectiveTrigger->add($triggerDuration);
} else {
if ('VTODO' === $parentComponent->name) {
if ($parentComponent instanceof VTodo) {
$endProp = 'DUE';
} elseif ('VEVENT' === $parentComponent->name) {
} elseif ($parentComponent instanceof VEvent) {
$endProp = 'DTEND';
} else {
throw new InvalidDataException('time-range filters on VALARM components are only supported when they are a child of VTODO or VEVENT');
Expand Down Expand Up @@ -78,8 +78,8 @@ public function getEffectiveTriggerTime()
* The rules used to determine if an event falls within the specified
* time-range is based on the CalDAV specification.
*
* @param DateTime $start
* @param DateTime $end
* @param \DateTime $start
* @param \DateTime $end
*
* @return bool
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/Component/VCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function expand(DateTimeInterface $start, DateTimeInterface $end, DateTim
} elseif ($child instanceof Component && 'VTIMEZONE' !== $child->name) {
// We're also stripping all VTIMEZONE objects because we're
// converting everything to UTC.
if ('VEVENT' === $child->name && (isset($child->{'RECURRENCE-ID'}) || isset($child->RRULE) || isset($child->RDATE))) {
if ($child instanceof VEvent && (isset($child->{'RECURRENCE-ID'}) || isset($child->RRULE) || isset($child->RDATE))) {
// Handle these a bit later.
$uid = (string) $child->UID;
if (!$uid) {
Expand All @@ -326,7 +326,7 @@ public function expand(DateTimeInterface $start, DateTimeInterface $end, DateTim
} else {
$recurringEvents[$uid] = [clone $child];
}
} elseif ('VEVENT' === $child->name && $child->isInTimeRange($start, $end)) {
} elseif ($child instanceof VEvent && $child->isInTimeRange($start, $end)) {
$newChildren[] = $stripTimezones(clone $child);
}
}
Expand Down Expand Up @@ -440,7 +440,7 @@ public function validate($options = 0)
if ($child instanceof Component) {
++$componentsFound;

if (!in_array($child->name, ['VEVENT', 'VTODO', 'VJOURNAL'])) {
if (!($child instanceof VEvent || $child instanceof VTodo || $child instanceof VJournal)) {
continue;
}
$componentTypes[] = $child->name;
Expand Down
4 changes: 3 additions & 1 deletion lib/Component/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public function getValidationRules()
* If neither of those parameters are specified, the first is returned, if
* a field with that name does not exist, null is returned.
*
* @param string $fieldName
* @param string $propertyName
*
* @return VObject\Property|null
*/
Expand Down Expand Up @@ -423,6 +423,8 @@ public function getByType($propertyName, $type)
return $field;
}
}

return null;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*
* @method Component create(string $name, array $children = null, $defaults = true)
* @method Property create($name, $value = null, array $parameters = null, $valueType = null)
*/
abstract class Document extends Component
{
Expand Down Expand Up @@ -124,9 +127,8 @@ public function getDocumentType()
* otherwise, we'll assume it's a property and call createProperty instead.
*
* @param string $name
* @param string $arg1,... Unlimited number of args
*
* @return mixed
* @return Node
*/
public function create($name)
{
Expand Down Expand Up @@ -244,6 +246,8 @@ public function getClassNameForPropertyValue($valueParam)
if (isset(static::$valueMap[$valueParam])) {
return static::$valueMap[$valueParam];
}

return null;
}

/**
Expand Down
Loading