Completed
Branch BUG/invalid-field-count (62be81)
by
unknown
78:11 queued 64:42
created

PersonalDataEraserManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 63
loc 63
rs 10
wmc 4
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A hookInErasers() 12 12 2
B loadPrivateDataEraserCollection() 24 24 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 EventEspresso\core\services\privacy\erasure;
4
5
use EventEspresso\core\exceptions\InvalidClassException;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
use EventEspresso\core\exceptions\InvalidEntityException;
8
use EventEspresso\core\exceptions\InvalidFilePathException;
9
use EventEspresso\core\exceptions\InvalidIdentifierException;
10
use EventEspresso\core\exceptions\InvalidInterfaceException;
11
use EventEspresso\core\services\collections\CollectionDetails;
12
use EventEspresso\core\services\collections\CollectionInterface;
13
use EventEspresso\core\services\collections\CollectionLoader;
14
15
/**
16
 * Class PersonalDataEraserManager
17
 * Manages setting up the hooks to add the EE core and add-ons' privacy erasers
18
 *
19
 * @package        Event Espresso
20
 * @author         Mike Nelson
21
 * @since          $VID:$
22
 */
23 View Code Duplication
class PersonalDataEraserManager
24
{
25
26
    public function __construct()
27
    {
28
        add_filter(
29
            'wp_privacy_personal_data_erasers',
30
            array($this, 'hookInErasers')
31
        );
32
    }
33
34
35
    /**
36
     * For all the registered `PrivateDataEraserInterface`s, add them as erasers
37
     */
38
    public function hookInErasers($erasers)
39
    {
40
        // load all the privacy policy stuff
41
        // add post policy text
42
        foreach ($this->loadPrivateDataEraserCollection() as $eraser) {
0 ignored issues
show
Bug introduced by
The expression $this->loadPrivateDataEraserCollection() of type object<EventEspresso\cor...alDataEraserInterface>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
43
            $erasers[ get_class($eraser) ] = array(
44
                'eraser_friendly_name' => $eraser->name(),
45
                'callback'             => array($eraser, 'erase'),
46
            );
47
        }
48
        return $erasers;
49
    }
50
51
52
    /**
53
     * @return CollectionInterface|PersonalDataEraserInterface[]
54
     * @throws InvalidIdentifierException
55
     * @throws InvalidInterfaceException
56
     * @throws InvalidFilePathException
57
     * @throws InvalidEntityException
58
     * @throws InvalidDataTypeException
59
     * @throws InvalidClassException
60
     */
61
    protected function loadPrivateDataEraserCollection()
62
    {
63
        $loader = new CollectionLoader(
64
            new CollectionDetails(
65
                // collection name
66
                'privacy_erasers',
67
                // collection interface
68
                'EventEspresso\core\services\privacy\erasure\PersonalDataEraserInterface',
69
                // FQCNs for classes to add (all classes within that namespace will be loaded)
70
                apply_filters(
71
                    'FHEE__EventEspresso_core_services_privacy_erasure_PersonalDataEraserManager__erasers',
72
                    array('EventEspresso\core\domain\services\admin\privacy\erasure')
73
                ),
74
                // filepaths to classes to add
75
                array(),
76
                // file mask to use if parsing folder for files to add
77
                '',
78
                // what to use as identifier for collection entities
79
                // using CLASS NAME prevents duplicates (works like a singleton)
80
                CollectionDetails::ID_CLASS_NAME
81
            )
82
        );
83
        return $loader->getCollection();
84
    }
85
}
86
// End of file PersonalDataEraserManager.php
87
// Location: EventEspresso\core\domain\services\admin/PersonalDataEraserManager.php
88