Skip to content

Commit b580c46

Browse files
committed
refactor: use the existing Server::DEPTH_INFINITY constant instead of string value
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent 7930b55 commit b580c46

File tree

6 files changed

+51
-32
lines changed

6 files changed

+51
-32
lines changed

lib/DAV/CorePlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public function httpMove(RequestInterface $request, ResponseInterface $response)
590590
$moveInfo = $this->server->getCopyAndMoveInfo($request);
591591

592592
// MOVE does only allow "infinity" every other header value is considered invalid
593-
if ('infinity' !== $moveInfo['depth']) {
593+
if (Server::DEPTH_INFINITY !== $moveInfo['depth']) {
594594
throw new BadRequest('The HTTP Depth header must only contain "infinity" for MOVE');
595595
}
596596

lib/DAV/ICopyTarget.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ interface ICopyTarget extends ICollection
2828
* is that the copy was successful.
2929
* If you return false, sabre/dav will handle the copy itself.
3030
*
31-
* @param string $targetName new local file/collection name
32-
* @param string $sourcePath Full path to source node
33-
* @param INode $sourceNode Source node itself
34-
* @param string|int $depth How many level of children to copy.
35-
* The value can be 'infinity' or a positiv number including zero.
36-
* Zero means to only copy a shallow collection with props but without children.
31+
* @param string $targetName new local file/collection name
32+
* @param string $sourcePath Full path to source node
33+
* @param INode $sourceNode Source node itself
34+
* @param int $depth How many level of children to copy.
35+
* The value can be 'infinity' (Sabre\DAV\Server::DEPTH_INFINITY) or a positive number including zero.
36+
* Zero means to only copy a shallow collection with props but without children.
3737
*
3838
* @return bool
3939
*/
40-
public function copyInto($targetName, $sourcePath, INode $sourceNode, $depth);
40+
public function copyInto($targetName, $sourcePath, INode $sourceNode, int $depth);
4141
}

lib/DAV/Server.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,10 @@ public function calculateUri($uri)
580580
/**
581581
* Returns the HTTP depth header.
582582
*
583-
* This method returns the contents of the HTTP depth request header. If the depth header was 'infinity' it will return the Sabre\DAV\Server::DEPTH_INFINITY object
583+
* This method returns the contents of the HTTP depth request header. If the depth header was 'infinity' it will return the Sabre\DAV\Server::DEPTH_INFINITY constant.
584584
* It is possible to supply a default depth value, which is used when the depth header has invalid content, or is completely non-existent
585585
*
586+
* @param mixed $default Default value to use if no header is set or has invalid value.
586587
* @param mixed $default
587588
*
588589
* @return int
@@ -726,13 +727,14 @@ public function getCopyAndMoveInfo(RequestInterface $request)
726727
}
727728
$destination = $this->calculateUri($request->getHeader('Destination'));
728729

729-
// Depth of inifinty is valid for MOVE and COPY. If it is not set the RFC requires to act like it was 'infinity'.
730-
$depth = strtolower($request->getHeader('Depth') ?? 'infinity');
731-
if ('infinity' !== $depth && is_numeric($depth)) {
730+
// Depth of infinity is valid for MOVE and COPY. If it is not set the RFC requires to act like it was 'infinity'.
731+
$depth = $request->getHeader('Depth') ?? 'infinity';
732+
if ('infinity' === strtolower($depth)) {
733+
$depth = self::DEPTH_INFINITY;
734+
} elseif (!ctype_digit($depth) || ((int) $depth) < 0) {
735+
throw new Exception\BadRequest('The HTTP Depth header may only be "infinity", 0 or an positive integer');
736+
} else {
732737
$depth = (int) $depth;
733-
if ($depth < 0) {
734-
throw new Exception\BadRequest('The HTTP Depth header may only be "infinity", 0 or a positive number');
735-
}
736738
}
737739

738740
$overwrite = $request->getHeader('Overwrite') ?? 'T';

lib/DAV/Tree.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ public function nodeExists($path)
135135
/**
136136
* Copies a file from path to another.
137137
*
138-
* @param string $sourcePath The source location
139-
* @param string $destinationPath The full destination path
140-
* @param int|string $depth How much levle of children to copy.
141-
* The value can be 'infinity' or a positiv integer, including zero.
142-
* Zero means only copy the collection without children but with its properties.
138+
* @param string $sourcePath The source location
139+
* @param string $destinationPath The full destination path
140+
* @param int $depth How many levels of children to copy.
141+
* The value can be 'infinity' (\Sabre\DAV\Server::DEPTH_INFINITY) or a positive integer, including zero.
142+
* Zero means only copy the collection without children but with its properties.
143143
*/
144-
public function copy($sourcePath, $destinationPath, $depth = 'infinity')
144+
public function copy($sourcePath, $destinationPath, int $depth = Server::DEPTH_INFINITY)
145145
{
146146
$sourceNode = $this->getNodeForPath($sourcePath);
147147

@@ -182,7 +182,7 @@ public function move($sourcePath, $destinationPath)
182182
}
183183
if (!$moveSuccess) {
184184
// Move is a copy with depth = infinity and deleting the source afterwards
185-
$this->copy($sourcePath, $destinationPath, 'infinity');
185+
$this->copy($sourcePath, $destinationPath, Server::DEPTH_INFINITY);
186186
$this->getNodeForPath($sourcePath)->delete();
187187
}
188188
}
@@ -310,10 +310,10 @@ public function getMultipleNodes($paths)
310310
/**
311311
* copyNode.
312312
*
313-
* @param string $destinationName
314-
* @param int|string $depth How many children of the node to copy
313+
* @param string $destinationName
314+
* @param int $depth How many children of the node to copy
315315
*/
316-
protected function copyNode(INode $source, ICollection $destinationParent, ?string $destinationName = null, $depth = 'infinity')
316+
protected function copyNode(INode $source, ICollection $destinationParent, ?string $destinationName = null, int $depth = Server::DEPTH_INFINITY)
317317
{
318318
if ('' === (string) $destinationName) {
319319
$destinationName = $source->getName();
@@ -340,7 +340,7 @@ protected function copyNode(INode $source, ICollection $destinationParent, ?stri
340340
// Copy children if depth is not zero
341341
if (0 !== $depth) {
342342
// Adjust next depth for children (keep 'infinity' or decrease)
343-
$depth = 'infinity' === $depth ? 'infinity' : $depth - 1;
343+
$depth = Server::DEPTH_INFINITY === $depth ? Server::DEPTH_INFINITY : $depth - 1;
344344
$destination = $destinationParent->getChild($destinationName);
345345
foreach ($source->getChildren() as $child) {
346346
$this->copyNode($child, $destination, null, $depth);

tests/Sabre/DAV/CorePluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testMoveSupportsDepth()
6262

6363
$server->expects($this->once())
6464
->method('getCopyAndMoveInfo')
65-
->willReturn(['depth' => 'infinity', 'destinationExists' => true, 'destination' => 'dst']);
65+
->willReturn(['depth' => Server::DEPTH_INFINITY, 'destinationExists' => true, 'destination' => 'dst']);
6666
$corePlugin->httpMove($request, $response);
6767
}
6868
}

tests/Sabre/DAV/ServerCopyMoveTest.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,33 @@
99

1010
class ServerCopyMoveTest extends AbstractServerTestCase
1111
{
12+
public function testMissingDestinationHeader()
13+
{
14+
$request = new HTTP\Request('COPY', '/', ['Depth' => 'infinity']);
15+
16+
$this->expectException(BadRequest::class);
17+
$this->expectExceptionMessage('The destination header was not supplied');
18+
$this->server->getCopyAndMoveInfo($request);
19+
}
20+
21+
public function testMissingDepthHeader()
22+
{
23+
$request = new HTTP\Request('COPY', '/', ['Destination' => '/destination']);
24+
25+
$this->assertEquals(Server::DEPTH_INFINITY, $this->server->getCopyAndMoveInfo($request)['depth']);
26+
}
27+
1228
/**
1329
* Only 'infinity' and positive (incl. 0) numbers are allowed.
1430
*
1531
* @dataProvider dataInvalidDepthHeader
1632
*/
17-
public function testInvalidDepthHeader(?string $headerValue)
33+
public function testInvalidDepthHeader(string $headerValue)
1834
{
19-
$request = new HTTP\Request('COPY', '/', null !== $headerValue ? ['Depth' => $headerValue] : []);
35+
$request = new HTTP\Request('COPY', '/', ['Destination' => '/destination', 'Depth' => $headerValue]);
2036

2137
$this->expectException(BadRequest::class);
38+
$this->expectExceptionMessage('The HTTP Depth header may only be "infinity", 0 or an positive integer');
2239
$this->server->getCopyAndMoveInfo($request);
2340
}
2441

@@ -51,15 +68,15 @@ public function dataDepthHeader()
5168
return [
5269
[
5370
[],
54-
'infinity',
71+
Server::DEPTH_INFINITY,
5572
],
5673
[
5774
['Depth' => 'infinity'],
58-
'infinity',
75+
Server::DEPTH_INFINITY,
5976
],
6077
[
6178
['Depth' => 'INFINITY'],
62-
'infinity',
79+
Server::DEPTH_INFINITY,
6380
],
6481
[
6582
['Depth' => '0'],

0 commit comments

Comments
 (0)