Completed
Push — master ( dcde2f...f2a084 )
by Simon
03:07 queued 12s
created

StandardPermissions::canPublish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
	/**
3
	 * @file
4
	 * Adds CMS defined permissions into the code.
5
	 */
6
	
7
	/**
8
	 * An extension which controls the 'can*' functions in an object.
9
	 */
10
	class StandardPermissions extends DataExtension implements PermissionProvider {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
	
12
		/**
13
		 * Can the user view this?
14
		 */
15
		public function canView($member = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $member 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...
16
			return Permission::check(get_class($this->owner) . '_view');
17
		}
18
	
19
		/**
20
		 * Can the user edit this?
21
		 */
22
		public function canEdit($member = false) {
23
			return Permission::check(get_class($this->owner) . '_edit');
24
		}
25
	
26
		/**
27
		 * Can the user delete this?
28
		 */
29
		public function canDelete($member = false) {
30
			return Permission::check(get_class($this->owner) . '_delete');
31
		}
32
	
33
		/**
34
		 * Can the user create this?
35
		 */
36
		public function canCreate($member = false) {
37
			return Permission::check(get_class($this->owner) . '_create');
38
		}
39
		
40
		/**
41
		 * Can the user publish this?
42
		 */
43
		public function canPublish($member = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $member 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...
44
			return Permission::check(get_class($this->owner) . '_publish');
45
		}
46
	
47
		/**
48
		 * Get a complete list of all the permissions this class uses.
49
		 */
50
		public function providePermissions() {
51
	
52
			// Prepare variables.
53
			$permissions = array();
54
	
55
			// For each class...
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
56
			foreach ($this->getClassList() as $class) {
57
	
58
				// ...add a few permissions.
59
				foreach (array(
60
					'view', 'edit', 'delete', 'create', 'publish',
61
				) as $name) {
62
	
63
					$permissions[$class . '_' . $name] = $class . '_' . $name;
64
				}
65
			}
66
	
67
			// Done.
68
			return $permissions;
69
		}
70
	
71
		/**
72
		 * Get a list of classes which 'we' extend.
73
		 */
74
		private function getClassList() {
75
	
76
			// Prepare variables.
77
			$classes = array();
78
	
79
			// Go through all the classes.
80
			foreach (ClassInfo::subclassesFor('DataObject') as $class => $file) {
0 ignored issues
show
Bug introduced by
The expression \ClassInfo::subclassesFor('DataObject') of type null|array 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...
81
	
82
				// What are the extensions for this class?
83
				$extensions = Config::inst()->get($class, 'extensions');
84
				if (!is_null($extensions)) {
85
	
86
					// Get a list of classes this horizontally extends this.
87
					if (in_array(get_class($this), $extensions)) {
88
						$classes[] = $class;
89
					}
90
				}
91
			}
92
	
93
			// Done.
94
			return $classes;
95
		}
96
	}
97