HackedFileHasher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hash() 0 15 3
A setCache() 0 4 1
A getCache() 0 10 2
A getCacheKey() 0 10 1
performHash() 0 1 ?
fetchLines() 0 1 ?
1
<?php
2
3
namespace Cerbere\Model\Hacked;
4
5
/**
6
 * Base class for the different ways that files can be hashed.
7
 *
8
 * Class HackedFileHasher
9
 * @package Cerbere\Model\Hacked
10
 */
11
abstract class HackedFileHasher
12
{
13
    /**
14
     * Returns a hash of the given filename.
15
     * Ignores file line endings
16
     *
17
     * @param string $filename
18
     * @return string|false
19
     */
20
    public function hash($filename)
21
    {
22
        if (file_exists($filename)) {
23
            if ($hash = $this->getCache($filename)) {
24
                return $hash;
25
            } else {
26
                $hash = $this->performHash($filename);
27
                $this->setCache($filename, $hash);
28
29
                return $hash;
30
            }
31
        }
32
33
        return false;
34
    }
35
36
    /**
37
     * @param string $filename
38
     * @param string $hash
39
     */
40
    public function setCache($filename, $hash)
0 ignored issues
show
Unused Code introduced by
The parameter $filename is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $hash is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        //cache_set($this->getCacheKey($filename), $hash, HACKED_CACHE_TABLE, strtotime('+7 days'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
    }
44
45
    /**
46
     * @param string $filename
47
     * @return string|false
48
     */
49
    public function getCache($filename)
0 ignored issues
show
Unused Code introduced by
The parameter $filename is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        $cache = false; //cache_get($this->getCacheKey($filename), HACKED_CACHE_TABLE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
53
        if (!empty($cache->data)) {
54
            return $cache->data;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * @param string $filename
62
     * @return string
63
     */
64
    public function getCacheKey($filename)
65
    {
66
        $key = array(
67
          'filename'   => $filename,
68
          'mtime'      => filemtime($filename),
69
          'class_name' => get_class($this),
70
        );
71
72
        return sha1(serialize($key));
73
    }
74
75
    /**
76
     * Compute and return the hash of the given file.
77
     *
78
     * @param string $filename
79
     *   A fully-qualified filename to hash.
80
     *
81
     * @return string
82
     *   The computed hash of the given file.
83
     */
84
    abstract public function performHash($filename);
85
86
    /**
87
     * Compute and return the lines of the given file.
88
     *
89
     * @param string $filename
90
     *   A fully-qualified filename to return.
91
     *
92
     * @return array|false
93
     *   The lines of the given filename or FALSE on failure.
94
     */
95
    abstract public function fetchLines($filename);
96
}
97