Completed
Pull Request — master (#8)
by Helpful
02:48
created

CommentNotifiableTest_DataObject   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4
Metric Value
wmc 4
lcom 0
cbo 4
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A notificationRecipients() 0 5 3
A Link() 0 6 1
1
<?php
2
3
class CommentNotifiableTest extends SapphireTest {
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...
4
5
	protected static $fixture_file = 'CommentNotifications.yml';
6
7
	protected $oldhost = null;
8
9
	protected $extraDataObjects = array(
10
		'CommentNotifiableTest_DataObject'
11
	);
12
	
13 View Code Duplication
	public function setUp() {
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...
Coding Style introduced by
setUp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
14
		parent::setUp();
15
		Config::nest();
16
		
17
		Config::inst()->update('Email', 'admin_email', '[email protected]');
18
		$this->oldhost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
19
		$_SERVER['HTTP_HOST'] = 'www.mysite.com';
20
	}
21
22
	public function tearDown() {
0 ignored issues
show
Coding Style introduced by
tearDown uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
23
		$_SERVER['HTTP_HOST'] = $this->oldhost;
24
		Config::unnest();
25
		parent::tearDown();
26
	}
27
28
	public function testGetRecipients() {
29
		$comment1 = $this->objFromFixture('Comment', 'comment1');
30
		$comment2 = $this->objFromFixture('Comment', 'comment2');
31
		$item1 = $this->objFromFixture('CommentNotifiableTest_DataObject', 'item1');
32
		$item2 = $this->objFromFixture('CommentNotifiableTest_DataObject', 'item2');
33
		$this->assertEquals(array('[email protected]'), $item1->notificationRecipients($comment1)->column('Email'));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifiableTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
		$this->assertEquals(array('[email protected]'), $item2->notificationRecipients($comment2));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifiableTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
	}
36
37
	public function testNotificationSubject() {
38
		$recipient = $this->objFromFixture('Member', 'author');
39
		$comment1 = $this->objFromFixture('Comment', 'comment1');
40
		$comment2 = $this->objFromFixture('Comment', 'comment2');
41
		$item1 = $this->objFromFixture('CommentNotifiableTest_DataObject', 'item1');
42
		$item2 = $this->objFromFixture('CommentNotifiableTest_DataObject', 'item2');
43
44
		$this->assertEquals('A new comment has been posted', $item1->notificationSubject($comment1, $recipient));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifiableTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
		$this->assertEquals('A new comment has been posted', $item2->notificationSubject($comment2, $recipient));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifiableTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
	}
47
48
	public function testNotificationSender() {
49
		$comment1 = $this->objFromFixture('Comment', 'comment1');
50
		$author = $this->objFromFixture('Member', 'author');
51
		$item1 = $this->objFromFixture('CommentNotifiableTest_DataObject', 'item1');
52
		$this->assertEquals('[email protected]', $item1->notificationSender($comment1, $author));
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentNotifiableTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
	}
54
55
}
56
57
/**
58
 * @mixin CommentNotifiable
59
 */
60
class CommentNotifiableTest_DataObject extends DataObject implements TestOnly {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
61
62
	private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
63
		"Title" => "Varchar(255)",
64
		"URLSegment" => "Varchar(255)",
65
	);
66
67
	private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
68
		'Author' => 'Member',
69
	);
70
71
	private static $extensions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $extensions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
72
		'CommentNotifiable',
73
		'CommentsExtension',
74
	);
75
76
	public function notificationRecipients($comment) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
77
		$author = $this->Author();
0 ignored issues
show
Documentation Bug introduced by
The method Author does not exist on object<CommentNotifiableTest_DataObject>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
78
		if($author && $author->exists()) return new ArrayList(array($author));
79
		return parent::notificationRecipients($comment);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class DataObject as the method notificationRecipients() does only exist in the following sub-classes of DataObject: CommentNotifiableTest_DataObject. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
80
	}
81
82
	public function Link($action = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $action 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...
83
		return Controller::join_links(
84
			Director::baseURL(),
85
			$this->URLSegment
0 ignored issues
show
Documentation introduced by
The property URLSegment does not exist on object<CommentNotifiableTest_DataObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
		);
87
	}
88
}
89