ExcelExport   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
c 3
b 0
f 0
lcom 0
cbo 3
dl 0
loc 118
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnLetter() 0 3 1
A printTitle() 0 4 1
A init() 0 6 1
A excel_export() 0 14 2
A cellColor() 0 8 1
A styleActiveSheet() 0 18 2
A downloadExcel() 0 16 1
A clearName() 0 9 2
A newSheet() 0 9 2
1
<?php
2
3
namespace Hospitalplugin\utils;
4
5
use Hospitalplugin\Entities\InfectionsCRUD;
6
use Hospitalplugin\Entities\Infections;
7
8
class ExcelExport {
9
	
10
	/**
11
	 */
12
	public static function init() {
13
		add_action ( 'admin_init', array (
14
				'Hospitalplugin\utils\ExcelExport',
15
				'excel_export' 
16
		), 1 );
17
	}
18
	/**
19
	 * 
20
	 */
21
	public static function getColumnLetter($num) {
22
		return chr ( 65 + $num );
23
	}
24
	/**
25
	 */
26
	static function excel_export() {
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...
Coding Style introduced by
excel_export uses the super-global variable $_POST 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...
27
		if (! isset ( $_POST ['excel'] )) {
28
			return;
29
		} else {
30
			// dataSetName: Infections, Punction
31
			$dataSetName = $_POST ['dataSetName'];
32
			$classname = '\Hospitalplugin\utils\ExcelExport' . $dataSetName;
33
		}
34
		
35
		$objPHPExcel = new \PHPExcel ();
36
		$classname::fillData ( $objPHPExcel );
37
		
38
		ExcelExport::downloadExcel ( $objPHPExcel, $dataSetName . '.xlsx' );
0 ignored issues
show
Documentation introduced by
$objPHPExcel is of type object<PHPExcel>, 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...
39
	}
40
	/**
41
	 */
42
	static function cellColor($objPHPExcel, $cells, $color) {
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...
43
		$objPHPExcel->getActiveSheet ()->getStyle ( $cells )->getFill ()->applyFromArray ( array (
44
				'type' => \PHPExcel_Style_Fill::FILL_SOLID,
45
				'startcolor' => array (
46
						'rgb' => $color 
47
				) 
48
		) );
49
	}
50
	/**
51
	 */
52
	static function styleActiveSheet($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...
53
		$objPHPExcel->getActiveSheet ()->getDefaultStyle ()->getFont ()->setName ( 'Arial' )->setSize ( 8 )->setBold ( false );
54
		$objPHPExcel->getActiveSheet ()->getDefaultStyle ()->getNumberFormat ()->setFormatCode ( \PHPExcel_Style_NumberFormat::FORMAT_TEXT );
55
		$objPHPExcel->getActiveSheet ()->freezePane ( 'A3' );
56
		$style = array (
57
				'alignment' => array (
58
						'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
59
						'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER 
60
				) 
61
		);
62
		$objWorksheet = $objPHPExcel->getActiveSheet ();
63
		$objWorksheet->getDefaultStyle ()->applyFromArray ( $style );
64
		$objWorksheet->getStyle ( 'A1:AA2' )->getFont ()->setBold ( true );
65
		for($col = ord ( 'a' ); $col <= ord ( 'z' ); $col ++) {
66
			$objWorksheet->getColumnDimension ( chr ( $col ) )->setAutoSize ( true );
67
		}
68
		return $objPHPExcel;
69
	}
70
	/**
71
	 *
72
	 * @param unknown $objPHPExcel        	
73
	 */
74
	private static function downloadExcel($objPHPExcel, $filename) {
75
		ob_end_clean ();
76
		ob_start ();
77
		header ( "Content-type: application/vnd.ms-excel; charset=utf-8" );
78
		header ( "Content-Transfer-Encoding: binary" );
79
		header ( "Content-Description: File Transfer" );
80
		header ( "Content-Disposition: attachment; filename=\"" . $filename . "\"" );
81
		header ( "Cache-Control: max-age=0" );
82
		header ( "Expires: 0" );
83
		header ( "Pragma: no-cache" );
84
		
85
		$objWriter = \PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel2007' );
86
		$objWriter->setPreCalculateFormulas ( true );
87
		$objWriter->save ( 'php://output' );
88
		exit ();
0 ignored issues
show
Coding Style Compatibility introduced by
The method downloadExcel() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
89
	}
90
	/**
91
	 * remove special chars and words from string 
92
	 * @param unknown $string        	
93
	 */
94
	private static function clearName($string, $removeWords) {
95
		$string = str_replace ( ' ', '-', $string );
96
		$string = preg_replace ( '/[^A-Za-z0-9\-]/', '', $string );
97
		foreach ( $removeWords as $word ) {
98
			$string = str_replace ( $word, '', $string );
99
		}
100
		$string = substr ( $string, 0, 29 );
101
		return $string;
102
	}
103
	/**
104
	 * @param unknown $objPHPExcel        	
105
	 * @param unknown $title        	
106
	 */
107
	static function printTitle($objPHPExcel, $title) {
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...
108
		$objPHPExcel->getActiveSheet ()->setTitle ( ExcelExport::clearName ( $title, array('ddzia') ), true );
109
		$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, 1, $title );
110
	}
111
	/**
112
	 *
113
	 * @param unknown $objPHPExcel
114
	 */
115
	static function newSheet($objPHPExcel, $index = -1) {
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...
116
		if ($index < 0) {
117
			$index = $objPHPExcel->getSheetCount ();
118
		}
119
		$objPHPExcel->createSheet ( $index );
120
		$objPHPExcel->setActiveSheetIndex ( $index );
121
		$sheet = $objPHPExcel->getActiveSheet ();
122
		return $sheet;
123
	}
124
	
125
}