Completed
Push — master ( 23e70a...a8735e )
by Mikael
06:05
created

CFileContent   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 11.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
c 4
b 0
f 0
lcom 1
cbo 2
dl 9
loc 76
ccs 0
cts 18
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 16 2
A setBasePath() 9 9 2
A setSuffix() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Anax\Content;
4
5
/**
6
 * A read content from filesystem.
7
 *
8
 */
9
class CFileContent
10
{
11
    use \Anax\TConfigure,
12
        \Anax\DI\TInjectionAware;
13
14
15
16
    /**
17
     * Base path to content.
18
     *
19
     */
20
    private $path;
21
22
23
24
    /**
25
     * Get file as content.
26
     *
27
     * @param string $file with content
28
     *
29
     * @return string as content of the file
30
     *
31
     * @throws Exception when file does not exist
32
     */
33
    public function get($file)
34
    {
35
        $basepath = $this->config['basepath'];
36
        $suffix = $this->config['suffix'];
37
        $target = "$basepath/$file{$suffix}";
38
39
        if (!is_readable($target)) {
40
            throw new \Exception(t("No such file content: @FILENAME", ["@FILENAME" => $target]));
41
        }
42
43
        $filter  = $this->config['textfilter'];
44
        $content = file_get_contents($target);
45
        $content = $this->di->textFilter->doFilter($content, $filter);
46
47
        return $content;
48
    }
49
50
51
52
    /**
53
     * Set base path where  to find content.
54
     *
55
     * @param string $path where content reside
56
     *
57
     * @return $this
58
     */
59 View Code Duplication
    public function setBasePath($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        if (!is_dir($path)) {
62
            throw new \Exception(t("Base path for file content is not a directory"));
63
        }
64
        $this->path = rtrim($path, '/') . '/';
65
66
        return $this;
67
    }
68
69
70
71
    /**
72
     * Set standard suffix.
73
     *
74
     * @param string $suffix to use as standard suffix of filename
75
     *
76
     * @return $this
77
     */
78
    public function setSuffix($suffix)
79
    {
80
        $this->config['suffix'] = $suffix;
81
82
        return $this;
83
    }
84
}
85