Completed
Pull Request — master (#133)
by
unknown
03:32
created

SRFMath::getNumbers()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
/**
4
 * Various mathematical functions - sum, product, average, min and max.
5
 *
6
 * @licence GNU GPL v3+
7
 * 
8
 * @author Jeroen De Dauw < [email protected] >
9
 * @author Yaron Koren
10
 * @author Nathan Yergler
11
 */
12
class SRFMath extends SMWResultPrinter {
13
14
	/**
15
	 * (non-PHPdoc)
16
	 * @see SMWResultPrinter::getName()
17
	 */
18
	public function getName() {
19
		// Give grep a chance to find the usages:
20
		// srf_printername_max, srf_printername_min, srf_printername_sum,
21
		// srf_printername_product, srf_printername_average, srf_printername_median
22
		return wfMessage( 'srf_printername_' . $this->mFormat )->text();
23
	}
24
25
	/**
26
	 * @see SMWResultPrinter::buildResult
27
	 *
28
	 * @since 1.8
29
	 *
30
	 * @param SMWQueryResult $results
31
	 *
32
	 * @return string
33
	 */
34
	protected function buildResult( SMWQueryResult $results ) {
35
		global $wgLang;
36
                $result=$this->getResultText( $results, SMW_OUTPUT_HTML );
37
                if (is_numeric($result))
38
		  return $wgLang->formatNum( $result);
39
                else
40
                  return $result;
41
	}
42
43
	/**
44
	 * @see SMWResultPrinter::getResultText()
45
	 */
46
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
47
		$numbers = $this->getNumbers( $res );
48
		
49
		if ( count( $numbers ) == 0 ) {
50
			return $this->params['default'];
51
		}
52
		
53
		switch ( $this->mFormat ) {
54
			case 'max':
55
				return max( $numbers );
56
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
57
			case 'min':
58
				return min( $numbers );
59
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
60
			case 'sum':
61
				return array_sum( $numbers );
62
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
63
			case 'product':
64
				return array_product( $numbers );
65
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
66
			case 'average':
67
				return array_sum( $numbers ) / count( $numbers );
68
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69
			case 'median':
70
				sort( $numbers, SORT_NUMERIC );
71
				$position = ( count( $numbers ) + 1 ) / 2 - 1;
72
				return ( $numbers[ceil( $position )] + $numbers[floor( $position )] ) / 2; 
73
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
74
		}
75
	}
76
	
77
	/**
78
	 * @param SMWQueryResult $res
79
	 * 
80
	 * @return float[]
81
	 */
82
	private function getNumbers( SMWQueryResult $res ) {
83
		$numbers = array();
84
85
		while ( $row = $res->getNext() ) {
86
			foreach( $row as $resultArray ) {
87
				foreach ( $resultArray->getContent() as $dataItem ) {
88
					self::addNumbersForDataItem( $dataItem, $numbers );
89
				}
90
			}
91
		}
92
93
		return $numbers;
94
	}
95
	
96
	/**
97
	 * @param SMWDataItem $dataItem
98
	 * @param float[] $numbers
99
	 */
100
	private function addNumbersForDataItem( SMWDataItem $dataItem, array &$numbers ) {
101
		switch ( $dataItem->getDIType() ) {
102
			case SMWDataItem::TYPE_NUMBER:
103
				$numbers[] = $dataItem->getNumber();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getNumber() does only exist in the following sub-classes of SMWDataItem: SMWDINumber. 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...
104
				break;
105
			case SMWDataItem::TYPE_BLOB:
106
				$numbers[] = $dataItem->getString();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataItem as the method getString() does only exist in the following sub-classes of SMWDataItem: SMWDIBlob, SMWDIError, SMWDIString. 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...
107
				break;
108
			case SMWDataItem::TYPE_CONTAINER:
109
				foreach ( $dataItem->getDataItems() as $di ) {
0 ignored issues
show
Bug introduced by
The method getDataItems() does not seem to exist on object<SMWDataItem>.

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...
110
					self::addNumbersForDataItem( $di, $numbers );
111
				}
112
				break;
113
			default:
114
		}
115
	}
116
117
}
118