ExcelExportPunction::printData()   B
last analyzed

Complexity

Conditions 6
Paths 13

Size

Total Lines 44
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 44
rs 8.439
cc 6
eloc 31
nc 13
nop 3
1
<?php
2
3
namespace Hospitalplugin\utils;
4
5
use Hospitalplugin\Entities\InfectionsCRUD;
6
use Hospitalplugin\Entities\Infections;
7
use Hospitalplugin\Entities\PatientRaport;
8
use Hospitalplugin\Entities\WardCRUD;
9
10
class ExcelExportPunction {
11
	
12
	/**
13
	 *
14
	 * @param unknown $id        	
15
	 */
16
	private static function getData($id, $type) {
17
		$raport = PatientRaport::getRaportBetweenDates ( $id, $type, '2014-06-01', (new \DateTime ())->format ( "Y-m-d" ) );
0 ignored issues
show
Documentation introduced by
'2014-06-01' is of type string, but the function expects a object<Hospitalplugin\Entities\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
(new \DateTime())->format('Y-m-d') is of type string, but the function expects a object<Hospitalplugin\Entities\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
18
		return $raport;
19
	}
20
	/**
21
	 *
22
	 * @param unknown $objPHPExcel        	
23
	 * @param unknown $cols        	
24
	 */
25
	private static function printHeaders($objPHPExcel, $cols) {
26
		$count = 1;
27
		$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, 2, "Data" );
28
		foreach ( $cols as $col ) {
29
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, 2, sprintf ( "%7s", $col ) );
30
		}
31
	}
32
	/**
33
	 */
34
	private static function printData($objPHPExcel, $data, $type) {
35
		$row = 3;
36
		$lastColumn = 1;
37
		$colNumberOfPatients = ExcelExport::getColumnLetter ( 1 );
38
		$indexes = PatientRaport::getIndexes ( $type );
39
		foreach ( $data as $rowKey => $rowValue ) {
40
			// 1 col: date
41
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, $row, json_encode ( $rowKey ) );
42
			$count = 1;
43
			// 2-5 cols: N1 N2 N3 N0
44
			foreach ( $indexes as $index ) {
45
				$value = isset ( $rowValue [$index] ) ? $rowValue [$index] : "0";
46
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, $row, $value );
47
			}
48
			// 6 col: num of categorized (N = N1+N2+N3)
49
			$colHighestCategory = ExcelExport::getColumnLetter ( $count - 2 );
50
			$colNumberOfPatients = ExcelExport::getColumnLetter ( $count );
51
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, $row, '=SUM(B' . $row . ':' . $colHighestCategory . $row . ')' );
52
			$tpb = PatientRaport::getTpb ( $type );
53
			// 7-11 tpb1, tpb2, tpb3, 0 (no-cat), 2 (add)
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...
54
			foreach ( $tpb as $tpbn ) {
0 ignored issues
show
Bug introduced by
The expression $tpb of type array<integer,integer,{"...r","3":"integer"}>|null 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...
55
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, $row, $tpbn );
56
			}
57
			// 9-11: Tpb1*N1,Tpb2*N2,Tpb3*N3+2xN
58
			$tpbSize = count ( $tpb );
59
			foreach ( $tpb as $tpbn ) {
0 ignored issues
show
Bug introduced by
The expression $tpb of type array<integer,integer,{"...r","3":"integer"}>|null 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...
60
				$col1Letter = ExcelExport::getColumnLetter ( $count - 2 * $tpbSize );
61
				$col2Letter = ExcelExport::getColumnLetter ( $count - $tpbSize );
62
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, $row, '=' . $col1Letter . $row . "*" . $col2Letter . $row );
63
			}
64
			// SUM Tpb
65
			$col3Letter = ExcelExport::getColumnLetter ( $count - $tpbSize );
66
			$col4Letter = ExcelExport::getColumnLetter ( $count - 1 );
67
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, $row, '=SUM(' . $col3Letter . $row . ":" . $col4Letter . $row . ")" );
68
			$row ++;
69
			$lastColumn = $count - 1;
70
		}
71
		// number of categorization days
72
		$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 1, 1, '=COUNTIFS(' . $colNumberOfPatients . '3:' . $colNumberOfPatients . ($row - 1) . ',">0")' );
73
		// sum
74
		$col5Letter = ExcelExport::getColumnLetter ( $lastColumn );
75
		$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 2, 1, '=SUM(' . $col5Letter . '3:' . $col5Letter . ($row - 1) . ')/60' );
76
		return $row;
77
	}
78
	
79
	/**
80
	 *
81
	 * @param unknown $objPHPExcel        	
82
	 */
83
	static function fillSummary($objPHPExcel) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
		ExcelExport::newSheet ( $objPHPExcel, 0 );
85
		ExcelExport::printTitle ( $objPHPExcel, "Raport" );
0 ignored issues
show
Documentation introduced by
'Raport' is of type string, but the function expects a object<Hospitalplugin\utils\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
		$sheetCount = $objPHPExcel->getSheetCount ();
87
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'A2', 'Oddz.' );
88
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'B2', 'Dni' );
89
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'C2', 'Tpb' );
90
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'D2', 'Tśpb' );
91
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'E2', 'Tśpc*' );
92
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'F2', 'Tśpc**' );
93
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'G2', 'Td***' );
94
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'H2', 'Le*' );
95
		$objPHPExcel->getActiveSheet ()->setCellValue ( 'I2', 'Le**' );
96
		for($i = 1; $i < $sheetCount; $i ++) {
97
			$loadedSheetNames = $objPHPExcel->getSheetNames ();
98
			foreach ( $loadedSheetNames as $sheetIndex => $loadedSheetName ) {
99
				if ($sheetIndex == 0) {
100
					continue;
101
				}
102
				$row = $sheetIndex + 2;
103
				$objPHPExcel->getActiveSheet ()->setCellValue ( 'A' . $row, "='" . $loadedSheetName . "'!A1" );
104
				$objPHPExcel->getActiveSheet ()->setCellValue ( 'B' . $row, "='" . $loadedSheetName . "'!B1" );
105
				$objPHPExcel->getActiveSheet ()->setCellValue ( 'C' . $row, "='" . $loadedSheetName . "'!C1" );
106
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 3, $row, "=IF(C" . $row . ">0,C" . $row . "/B" . $row . ",0)" );
107
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 4, $row, "=D" . $row . "*110%" );
108
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 5, $row, "=D" . $row . "*125%" );
109
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 6, $row, "1531" );
110
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 7, $row, "=IF(E" . $row . ">0,E" . $row . "*365/G" . $row . ",\"-\")" );
111
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 8, $row, "=IF(F" . $row . ">0,F" . $row . "*365/G" . $row . ",\"-\")" );
112
			}
113
			$objPHPExcel->getActiveSheet ()->getStyle ( 'C3:I100' )->getNumberFormat ()->setFormatCode ( '#,##0.0' );
114
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, $row + 1, "* Czas pielęgnacji pośredniej jako 10% czasu pielęgnacji bezpośredniej" );
0 ignored issues
show
Bug introduced by
The variable $row does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
115
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, $row + 2, "** Czas pielęgnacji pośredniej jako 25% czasu pielęgnacji bezpośredniej" );
116
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, $row + 3, "*** Czas dyspozycyjny - propozycja z Rozporządzenia MZ: 202 dni x 7,58 h" );
117
			ExcelExport::styleActiveSheet ( $objPHPExcel );
118
		}
119
	}
120
	static function fillData($objPHPExcel) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
121
		// no time limit for this script
122
		set_time_limit ( 0 );
123
		$time_start = microtime ( true );
124
		// remove first default sheet
125
		$objPHPExcel->removeSheetByIndex ( 0 );
126
		// get wards
127
		$wards = WardCRUD::getWardsArray ();
128
		foreach ( $wards as $ward ) {
129
			ExcelExport::newSheet ( $objPHPExcel );
130
			$wardName = $ward->name . " (" . $ward->getTypOddzialu () . ")";
131
			ExcelExport::printTitle ( $objPHPExcel, $wardName );
0 ignored issues
show
Documentation introduced by
$wardName is of type string, but the function expects a object<Hospitalplugin\utils\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
			$data = ExcelExportPunction::getData ( $ward->id, $ward->getTypOddzialu () );
133
			$cols = PatientRaport::getColumns ( $ward->getTypOddzialu () );
134
			ExcelExportPunction::printHeaders ( $objPHPExcel, $cols );
0 ignored issues
show
Documentation introduced by
$cols is of type array<integer,string,{"0...string","15":"string"}>, but the function expects a object<Hospitalplugin\utils\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
			ExcelExportPunction::printData ( $objPHPExcel, $data, $ward->getTypOddzialu () );
136
			ExcelExport::styleActiveSheet ( $objPHPExcel );
137
		}
138
		ExcelExportPunction::fillSummary ( $objPHPExcel );
139
		$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, 100, "summary " . ($time_start - microtime ( true )) . " s" );
140
	}
141
}