MsProjectMPX   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 3
dl 0
loc 235
ccs 109
cts 109
cp 1
rs 9.68
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 35 5
A sanitizeProject() 0 22 5
A sanitizeTask() 0 14 5
B sanitizeTaskParent() 0 32 7
A writeRecordMPX() 0 4 1
A writeRecord30() 0 4 1
A writeRecord40() 0 4 1
A writeRecord41() 0 4 1
A writeRecord50() 0 4 1
A writeRecord60() 0 4 1
A writeRecord61() 0 4 1
A writeRecord70() 0 12 3
A writeRecord75() 0 4 1
1
<?php
2
/**
3
 * This file is part of PHPProject - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPProject is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPProject
14
 * @copyright   2009-2014 PHPProject contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpProject\Writer;
19
20
use PhpOffice\PhpProject\PhpProject;
21
use PhpOffice\PhpProject\Resource;
22
use PhpOffice\PhpProject\Task;
23
24
/**
25
 * MsProjectMPx
26
 *
27
 * @category    PHPProject
28
 * @package        PHPProject
29
 * @copyright    Copyright (c) 2012 - 2012 PHPProject (https://github.com/PHPOffice/PHPProject)
30
 */
31
class MsProjectMPX
32
{
33
    /**
34
     * PHPProject object
35
     *
36
     * @var \PhpOffice\PhpProject\PhpProject
37
     */
38
    private $phpProject;
39
    
40
    /**
41
     * Content to write in File
42
     * @var string[]
43
     */
44
    private $fileContent = array();
45
    
46
    
47
    /**
48
     * Create a new PHPProject_Writer_GanttProject
49
     *
50
     * @param    PHPProject    $phpProject
51
     */
52 2
    public function __construct(PhpProject $phpProject)
53
    {
54 2
        $this->phpProject = $phpProject;
55 2
    }
56
    
57
    /**
58
     * 
59
     * @param string $pFilename
60
     * @throws Exception
61
     */
62 2
    public function save($pFilename)
63
    {
64 2
        $arrProjectInfo = $this->sanitizeProject();
65
        
66 2
        $this->writeRecordMPX();
67
        // Project Header
68 2
        $this->writeRecord30($arrProjectInfo);
69
        // Text Resource Table Definition
70 2
        $this->writeRecord40();
71
        // Numeric Resource Table Definition
72 2
        $this->writeRecord41();
73
        // Resources
74 2
        foreach ($this->phpProject->getAllResources() as $oResource) {
75 1
            $this->writeRecord50($oResource);
0 ignored issues
show
Documentation introduced by
$oResource is of type resource, but the function expects a object<PhpOffice\PhpProject\Resource>.

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...
76 2
        }
77
        // Text Task Table Definition
78 2
        $this->writeRecord60();
79
        // Numeric Task Table Definition
80 2
        $this->writeRecord61();
81
        // Tasks
82 2
        foreach ($this->phpProject->getAllTasks() as $oTask) {
83 2
            $this->writeRecord70($oTask);
84 2
        }
85
        
86
        // Writing XML Object in file
87
        // Open file
88 2
        if (file_exists($pFilename) && !is_writable($pFilename)) {
89 1
            throw new \Exception("Could not open file $pFilename for writing.");
90
        }
91 1
        $fileHandle = fopen($pFilename, 'wb+');
92
        // Write Content
93 1
        fwrite($fileHandle, implode(PHP_EOL, $this->fileContent));
94
        // Close file
95 1
        fclose($fileHandle);
96 1
    }
97
    
98
    /**
99
     * @return multitype:Ambigous <number, unknown>
0 ignored issues
show
Documentation introduced by
The doc-type multitype:Ambigous could not be parsed: Unknown type name "multitype:Ambigous" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
100
     */
101 2
    private function sanitizeProject()
102
    {
103
        // Info Project
104 2
        $minDate = 0;
105
        // Browse all tasks
106 2
        $arrTasks = $this->phpProject->getAllTasks();
107 2
        foreach ($arrTasks as $oTask) {
108 2
            if ($oTask->getTaskCount() == 0) {
109 2
                $this->sanitizeTask($oTask);
110 2
            } else {
111 1
                $this->sanitizeTaskParent($oTask);
112
            }
113 2
            $tStartDate = $oTask->getStartDate();
114 2
            if ($minDate == 0 || $tStartDate < $minDate) {
115 2
                $minDate = $tStartDate;
116 2
            }
117 2
        }
118
        
119
        return array(
120
            'date_start' => (int)$minDate
121 2
        );
122
    }
123
    
124
    /**
125
     * Permits to clean a task
126
     * - If the duration is not filled, but the end date is, we calculate it.
127
     * - If the end date is not filled, but the duration is, we calculate it.
128
     * @param PHPProject_Task $oTask
129
     */
130 2
    private function sanitizeTask(Task $oTask)
131
    {
132 2
        $pDuration = $oTask->getDuration();
133 2
        $pEndDate = $oTask->getEndDate();
134 2
        $pStartDate = $oTask->getStartDate();
135
        
136 2
        if (is_null($pDuration) && !is_null($pEndDate)) {
137 1
            $iTimeDiff = $pEndDate - $pStartDate;
138 1
            $iNumDays = $iTimeDiff / 60 / 60 / 24;
139 1
            $oTask->setDuration($iNumDays + 1);
140 2
        } elseif (!is_null($pDuration) && is_null($pEndDate)) {
141 1
            $oTask->setEndDate($pStartDate + ($pDuration * 24 * 60 * 60));
142 1
        }
143 2
    }
144
    
145
    /**
146
     * Permits to clean parent task and calculate parent data like total duration,
147
     *   date start and complete average.
148
     * @param PHPProject_Task $oParentTask
149
     */
150 1
    private function sanitizeTaskParent(Task $oParentTask)
151
    {
152 1
        $arrTasksChilds = $oParentTask->getTasks();
153
        
154 1
        $iProgress = 0;
155 1
        $tStartDate = null;
156 1
        $tEndDate = null;
157 1
        foreach ($arrTasksChilds as $oTaskChild) {
158 1
            if ($oTaskChild->getTaskCount() == 0) {
159 1
                $this->sanitizeTask($oTaskChild);
160 1
            } else {
161 1
                $this->sanitizeTaskParent($oTaskChild);
162
            }
163
            
164 1
            $iProgress += $oTaskChild->getProgress();
165 1
            if (is_null($tStartDate)) {
166 1
                $tStartDate = $oTaskChild->getStartDate();
167 1
            } elseif ($tStartDate > $oTaskChild->getStartDate()) {
168 1
                $tStartDate = $oTaskChild->getStartDate();
169 1
            }
170
            
171 1
            if (is_null($tEndDate)) {
172 1
                $tEndDate = $oTaskChild->getEndDate();
173 1
            } elseif ($tEndDate < $oTaskChild->getEndDate()) {
174 1
                $tEndDate = $oTaskChild->getEndDate();
175 1
            }
176 1
        }
177 1
        $oParentTask->setProgress($iProgress / $oParentTask->getTaskCount());
178 1
        $oParentTask->setStartDate($tStartDate);
179 1
        $oParentTask->setEndDate($tEndDate);
180 1
        $oParentTask->setDuration((($tEndDate - $tStartDate) / 60 / 60 / 24) + 1);
181 1
    }
182
183
    /**
184
     * Record MPX
185
     */
186 2
    private function writeRecordMPX()
187
    {
188 2
        $this->fileContent[] = 'MPX;Microsoft Project for Windows;4.0;ANSI';
189 2
    }
190
    
191
    /**
192
     * Record "Project Header"
193
     */
194 2
    private function writeRecord30(array $arrProjectInfo)
195
    {
196 2
        $this->fileContent[] = '30;Project1;;;Standard;'.date('d/m/Y', $arrProjectInfo['date_start']).';;0;'.date('d/m/Y').';;$0,00;$0,00;$0,00;0h;0h;0h;0%;0d;0d;0d;0%;;;;;0d;0d';
197 2
    }
198
    
199
    /**
200
     * Record "Text Resource Table Definition"
201
     */
202 2
    private function writeRecord40()
203
    {
204 2
        $this->fileContent[] = '40;ID;Name';
205 2
    }
206
    
207
    /**
208
     * Record "Numeric Resource Table Definition"
209
     */
210 2
    private function writeRecord41()
211
    {
212 2
        $this->fileContent[] = '41;40;1';
213 2
    }
214
215
    /**
216
     * Record "Resource"
217
     * @param Resource $oResource
218
     */
219 1
    private function writeRecord50(Resource $oResource)
220
    {
221 1
        $this->fileContent[] = '50;'.$oResource->getIndex().';'.$oResource->getTitle();
222 1
    }
223
224
    /**
225
     * Record "Text Task Table Definition"
226
     */
227 2
    private function writeRecord60()
228
    {
229 2
        $this->fileContent[] = '60;ID;Name;Duration;% Complete;Start';
230 2
    }
231
    
232
    /**
233
     * Record "Numeric Task Table Definition"
234
     */
235 2
    private function writeRecord61()
236
    {
237 2
        $this->fileContent[] = '61;90;1;40;44;50';
238 2
    }
239
240
    /**
241
     * Record "Task"
242
     * @param Task $oTask
243
     */
244 2
    private function writeRecord70(Task $oTask)
245
    {
246 2
        $this->fileContent[] = '70;'.$oTask->getIndex().';'.$oTask->getName().';'.$oTask->getDuration().'d;'.number_format($oTask->getProgress(), 1).';'.date('d/m/Y', $oTask->getStartDate());
247
        
248 2
        foreach ($oTask->getResources() as $oResource) {
249 1
            $this->writeRecord75($oResource);
0 ignored issues
show
Documentation introduced by
$oResource is of type integer, but the function expects a object<PhpOffice\PhpProject\Resource>.

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...
250 2
        }
251
        
252 2
        foreach ($oTask->getTasks() as $oSubTask) {
253 1
            $this->writeRecord70($oSubTask);
254 2
        }
255 2
    }
256
    
257
    /**
258
     * Record "Resource Assignment"
259
     * @param Resource $oResource
260
     */
261 1
    private function writeRecord75(Resource $oResource)
262
    {
263 1
        $this->fileContent[] = '75;'.$oResource->getIndex().';1;;;;;;;;;;;';
264 1
    }
265
}
266