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 1 and other fixes #1329

Merged
merged 3 commits into from
Feb 11, 2021
Merged
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
1 change: 1 addition & 0 deletions lib/CalDAV/Schedule/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ public function scheduleLocalDelivery(ITip\Message $iTipMessage)

$currentObject = null;
$objectNode = null;
$oldICalendarData = null;
$isNewNode = false;

$result = $home->getCalendarObjectByUID($uid);
Expand Down
3 changes: 3 additions & 0 deletions lib/DAV/Browser/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public function httpGet(RequestInterface $request, ResponseInterface $response)
public function httpPOST(RequestInterface $request, ResponseInterface $response)
{
$contentType = $request->getHeader('Content-Type');
if (!\is_string($contentType)) {
return;
}
list($contentType) = explode(';', $contentType);
if ('application/x-www-form-urlencoded' !== $contentType &&
'multipart/form-data' !== $contentType) {
Expand Down
5 changes: 4 additions & 1 deletion lib/DAV/FSExt/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function put($data)
*
* The second argument is the type of update we're doing.
* This is either:
* * 1. append
* * 1. append (default)
* * 2. update based on a start byte
* * 3. update based on an end byte
*;
Expand Down Expand Up @@ -75,6 +75,9 @@ public function patch($data, $rangeType, $offset = null)
$f = fopen($this->path, 'c');
fseek($f, $offset, SEEK_END);
break;
default:
Copy link
Contributor Author

@phil-davis phil-davis Feb 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan reported that $f might not be defined below.
If the caller passes something other than 1 2 or 3 what should we do?
(I made it do gthe same thing as if they passed in 1)

$f = fopen($this->path, 'a');
break;
}
if (is_string($data)) {
fwrite($f, $data);
Expand Down
2 changes: 2 additions & 0 deletions lib/DAV/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ protected function copyNode(INode $source, ICollection $destinationParent, $dest
$destinationName = $source->getName();
}

$destination = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan reports that $destination might not be defined. So set it here, just in case.


if ($source instanceof IFile) {
$data = $source->get();

Expand Down
3 changes: 0 additions & 3 deletions lib/DAV/Xml/Property/Href.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class Href implements Element, HtmlOutput
*
* You must either pass a string for a single href, or an array of hrefs.
*
* If auto-prefix is set to false, the hrefs will be treated as absolute
* and not relative to the servers base uri.
*
Comment on lines -42 to -44
Copy link
Contributor Author

@phil-davis phil-davis Feb 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor had the 2nd parameter removed some time ago. So remove the PHPdoc. And fix the old calls in the test code...

* @param string|string[] $hrefs
*/
public function __construct($hrefs)
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
parameters:
level: 0
level: 1
bootstrapFiles:
- tests/bootstrap.php
14 changes: 7 additions & 7 deletions tests/Sabre/CalDAV/Backend/AbstractPDOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ public function testGetChangesBadId()
public function testCreateSubscriptions()
{
$props = [
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics'),
'{DAV:}displayname' => 'cal',
'{http://apple.com/ns/ical/}refreshrate' => 'P1W',
'{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
Expand Down Expand Up @@ -1004,7 +1004,7 @@ public function testCreateSubscriptionFail()
public function testUpdateSubscriptions()
{
$props = [
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics'),
'{DAV:}displayname' => 'cal',
'{http://apple.com/ns/ical/}refreshrate' => 'P1W',
'{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
Expand All @@ -1018,7 +1018,7 @@ public function testUpdateSubscriptions()

$newProps = [
'{DAV:}displayname' => 'new displayname',
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics', false),
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics'),
];

$propPatch = new DAV\PropPatch($newProps);
Expand Down Expand Up @@ -1046,7 +1046,7 @@ public function testUpdateSubscriptions()
public function testUpdateSubscriptionsFail()
{
$props = [
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics'),
'{DAV:}displayname' => 'cal',
'{http://apple.com/ns/ical/}refreshrate' => 'P1W',
'{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
Expand All @@ -1060,7 +1060,7 @@ public function testUpdateSubscriptionsFail()

$propPatch = new DAV\PropPatch([
'{DAV:}displayname' => 'new displayname',
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics', false),
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics'),
'{DAV:}unknown' => 'foo',
]);

Expand All @@ -1077,7 +1077,7 @@ public function testUpdateSubscriptionsFail()
public function testDeleteSubscriptions()
{
$props = [
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics', false),
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal.ics'),
'{DAV:}displayname' => 'cal',
'{http://apple.com/ns/ical/}refreshrate' => 'P1W',
'{http://apple.com/ns/ical/}calendar-color' => '#FF00FFFF',
Expand All @@ -1091,7 +1091,7 @@ public function testDeleteSubscriptions()

$newProps = [
'{DAV:}displayname' => 'new displayname',
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics', false),
'{http://calendarserver.org/ns/}source' => new \Sabre\DAV\Xml\Property\Href('http://example.org/cal2.ics'),
];

$backend->deleteSubscription(1);
Expand Down
2 changes: 0 additions & 2 deletions tests/Sabre/CalDAV/Schedule/SchedulingObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function testInvalidArg1()
$this->expectException('InvalidArgumentException');
$obj = new SchedulingObject(
new Backend\MockScheduling([], []),
[],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SchedulingObject only takes 2 parameters in the constructor. The test code is out-of-date.

[]
);
}
Expand All @@ -85,7 +84,6 @@ public function testInvalidArg2()
$this->expectException('InvalidArgumentException');
$obj = new SchedulingObject(
new Backend\MockScheduling([], []),
[],
['calendarid' => '1']
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Sabre/CalDAV/Subscriptions/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getSub($override = [])
$caldavBackend = new \Sabre\CalDAV\Backend\MockSubscriptionSupport([], []);

$info = [
'{http://calendarserver.org/ns/}source' => new Href('http://example.org/src', false),
'{http://calendarserver.org/ns/}source' => new Href('http://example.org/src'),
'lastmodified' => date('2013-04-06 11:40:00'), // tomorrow is my birthday!
'{DAV:}displayname' => 'displayname',
];
Expand All @@ -43,7 +43,7 @@ public function testValues()
$this->assertEquals(
[
'{DAV:}displayname' => 'displayname',
'{http://calendarserver.org/ns/}source' => new Href('http://example.org/src', false),
'{http://calendarserver.org/ns/}source' => new Href('http://example.org/src'),
],
$sub->getProperties(['{DAV:}displayname', '{http://calendarserver.org/ns/}source'])
);
Expand Down
9 changes: 9 additions & 0 deletions tests/Sabre/DAV/Browser/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ public function testPostOtherContentType()
$this->assertEquals(501, $this->response->status);
}

public function testPostNoContentType()
{
$request = new HTTP\Request('POST', '/', []);
$this->server->httpRequest = $request;
$this->server->exec();

$this->assertEquals(501, $this->response->status);
}

public function testPostNoSabreAction()
{
$request = new HTTP\Request('POST', '/', ['Content-Type' => 'application/x-www-form-urlencoded']);
Expand Down
7 changes: 7 additions & 0 deletions tests/Sabre/DAV/DbTestHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function getDb()
throw new \Exception('You must set the $driver public property');
}

$pdo = null;

if (array_key_exists($this->driver, DbCache::$cache)) {
$pdo = DbCache::$cache[$this->driver];
if (null === $pdo) {
Expand Down Expand Up @@ -58,6 +60,11 @@ public function getDb()
}
break;
}

if (null === $pdo) {
$this->markTestSkipped($this->driver.' was not recognised');
}

$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan reported that $pdo might not be set at this point, which is technically possible. Add some code above to deal with that case.

} catch (PDOException $e) {
$this->markTestSkipped($this->driver.' was not enabled or not correctly configured. Error message: '.$e->getMessage());
Expand Down
29 changes: 28 additions & 1 deletion tests/Sabre/DAV/FSExt/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ public function testPut()
);
}

public function testRange()
public function testRangeAppend()
{
$file = new File(SABRE_TEMPDIR.'/file.txt');
$file->put('0000000');
$file->patch('111', 1);

$this->assertEquals('0000000111', file_get_contents(SABRE_TEMPDIR.'/file.txt'));
}

public function testRangeOffset()
{
$file = new File(SABRE_TEMPDIR.'/file.txt');
$file->put('0000000');
Expand All @@ -43,6 +52,24 @@ public function testRange()
$this->assertEquals('0001110', file_get_contents(SABRE_TEMPDIR.'/file.txt'));
}

public function testRangeOffsetEnd()
{
$file = new File(SABRE_TEMPDIR.'/file.txt');
$file->put('0000000');
$file->patch('11', 3, -4);

$this->assertEquals('0001100', file_get_contents(SABRE_TEMPDIR.'/file.txt'));
}

public function testRangeOffsetDefault()
{
$file = new File(SABRE_TEMPDIR.'/file.txt');
$file->put('0000000');
$file->patch('11', 0);

$this->assertEquals('000000011', file_get_contents(SABRE_TEMPDIR.'/file.txt'));
}

public function testRangeStream()
{
$stream = fopen('php://memory', 'r+');
Expand Down
2 changes: 1 addition & 1 deletion tests/Sabre/DAV/SyncTokenPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function data()
],
[
'{DAV:}sync-token',
new Xml\Property\Href(Sync\Plugin::SYNCTOKEN_PREFIX.'hello', false),
new Xml\Property\Href(Sync\Plugin::SYNCTOKEN_PREFIX.'hello'),
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Sabre/DAV/Xml/Element/PropTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testDeserializeCustom()
XML;

$expected = [
'{DAV:}foo' => new Href('/hello', false),
'{DAV:}foo' => new Href('/hello'),
];

$elementMap = [
Expand Down
2 changes: 1 addition & 1 deletion tests/Sabre/DAV/Xml/Element/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function testSerializeComplexProperty()
{
$innerProps = [
200 => [
'{DAV:}link' => new DAV\Xml\Property\Href('http://sabredav.org/', false),
'{DAV:}link' => new DAV\Xml\Property\Href('http://sabredav.org/'),
],
];

Expand Down
2 changes: 1 addition & 1 deletion tests/Sabre/DAV/Xml/Property/HrefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testUnserializeEmpty()
*/
public function testSerializeEntity()
{
$href = new Href('http://example.org/?a&b', false);
$href = new Href('http://example.org/?a&b');
$this->assertEquals('http://example.org/?a&b', $href->getHref());

$xml = $this->write(['{DAV:}anything' => $href]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Sabre/DAVACL/PluginUpdatePropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testSetGroupMembers()
$server->addPlugin($plugin);

$result = $server->updateProperties('foo', [
'{DAV:}group-member-set' => new DAV\Xml\Property\Href(['/bar', '/baz'], true),
'{DAV:}group-member-set' => new DAV\Xml\Property\Href(['/bar', '/baz']),
]);

$expected = [
Expand Down Expand Up @@ -99,7 +99,7 @@ public function testSetBadNode()
$server->addPlugin($plugin);

$result = $server->updateProperties('foo', [
'{DAV:}group-member-set' => new DAV\Xml\Property\Href(['/bar', '/baz'], false),
'{DAV:}group-member-set' => new DAV\Xml\Property\Href(['/bar', '/baz']),
]);

$expected = [
Expand Down
2 changes: 2 additions & 0 deletions tests/Sabre/DAVACL/PrincipalBackend/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public function setGroupMemberSet($path, array $members)
public function updatePrincipal($path, \Sabre\DAV\PropPatch $propPatch)
{
$value = null;
$principal = false;
$principalIndex = null;
Comment on lines +134 to +135
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpstan reports that these might not be defined, so initiialize them just in case the foreach has an empty loop.

foreach ($this->principals as $principalIndex => $value) {
if ($value['uri'] === $path) {
$principal = $value;
Expand Down
3 changes: 0 additions & 3 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
<testsuite name="sabre-http">
<directory>../vendor/sabre/http/tests/HTTP</directory>
</testsuite>
<testsuite name="sabre-vobject">
<directory>../vendor/sabre/vobject/tests/VObject</directory>
</testsuite>

<testsuite name="sabre-dav">
<directory>Sabre/DAV</directory>
Expand Down