Completed
Push — develop ( 2af103...91b3d7 )
by Franck
12s
created

File::realpath()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 2
nop 1
crap 8
1
<?php
2
/**
3
 * This file is part of PHPOffice Common
4
 *
5
 * PHPOffice Common is free software distributed under the terms of the GNU Lesser
6
 * General Public License version 3 as published by the Free Software Foundation.
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code. For the full list of
10
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
11
 *
12
 * @link        https://github.com/PHPOffice/Common
13
 * @copyright   2009-2016 PHPOffice Common contributors
14
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15
 */
16
17
namespace PhpOffice\Common;
18
19
/**
20
 * Drawing
21
 */
22
class File
23
{
24
    /**
25
     * Verify if a file exists
26
     *
27
     * @param  string $pFilename Filename
28
     * @return bool
29
     */
30 2
    public static function fileExists($pFilename)
31
    {
32
        // Sick construction, but it seems that
33
        // file_exists returns strange values when
34
        // doing the original file_exists on ZIP archives...
35 2
        if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
36
            // Open ZIP file and verify if the file exists
37 2
            $zipFile     = substr($pFilename, 6, strpos($pFilename, '#') - 6);
38 2
            $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
39
40 2
            $zip = new \ZipArchive();
41 2
            if ($zip->open($zipFile) === true) {
42 2
                $returnValue = ($zip->getFromName($archiveFile) !== false);
43 2
                $zip->close();
44
45 2
                return $returnValue;
46
            }
47
48 2
            return false;
49
        }
50
51
        // Regular file_exists
52 2
        return file_exists($pFilename);
53
    }
54
    /**
55
     * Returns the content of a file
56
     *
57
     * @param  string $pFilename Filename
58
     * @return string
59
     */
60 1
    public static function fileGetContents($pFilename)
61
    {
62 1
        if (!self::fileExists($pFilename)) {
63 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PhpOffice\Common\File::fileGetContents of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
64
        }
65 1
        if (strtolower(substr($pFilename, 0, 3)) == 'zip') {
66
            // Open ZIP file and verify if the file exists
67 1
            $zipFile     = substr($pFilename, 6, strpos($pFilename, '#') - 6);
68 1
            $archiveFile = substr($pFilename, strpos($pFilename, '#') + 1);
69
70 1
            $zip = new \ZipArchive();
71 1
            if ($zip->open($zipFile) === true) {
72 1
                $returnValue = $zip->getFromName($archiveFile);
73 1
                $zip->close();
74 1
                return $returnValue;
75
            }
76
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PhpOffice\Common\File::fileGetContents of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
77
        }
78
        // Regular file contents
79 1
        return file_get_contents($pFilename);
80
    }
81
82
    /**
83
     * Returns canonicalized absolute pathname, also for ZIP archives
84
     *
85
     * @param  string $pFilename
86
     * @return string
87
     */
88 1
    public static function realpath($pFilename)
89
    {
90
        // Try using realpath()
91 1
        $returnValue = realpath($pFilename);
92
93
        // Found something?
94 1
        if ($returnValue == '' || is_null($returnValue)) {
95 1
            $pathArray = explode('/', $pFilename);
96 1
            while (in_array('..', $pathArray) && $pathArray[0] != '..') {
97 1
                $numPathArray = count($pathArray);
98 1
                for ($i = 0; $i < $numPathArray; ++$i) {
99 1
                    if ($pathArray[$i] == '..' && $i > 0) {
100 1
                        unset($pathArray[$i]);
101 1
                        unset($pathArray[$i - 1]);
102 1
                        break;
103
                    }
104 1
                }
105 1
            }
106 1
            $returnValue = implode('/', $pathArray);
107 1
        }
108
109
        // Return
110 1
        return $returnValue;
111
    }
112
}
113