Completed
Push — develop ( e1f81f...539a89 )
by Adrien
16:11
created

ContentTypes::writeOverrideContentType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 2
nop 3
dl 12
loc 12
rs 9.4285
c 1
b 0
f 0
1
<?php
2
namespace PhpSpreadsheet\Writer\Excel2007;
3
4
/**
5
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
 *
21
 * @category   PhpSpreadsheet
22
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
23
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
24
 * @version    ##VERSION##, ##DATE##
25
 */
26
class ContentTypes extends WriterPart
27
{
28
    /**
29
     * Write content types to XML format
30
     *
31
     * @param     \PhpSpreadsheet\Spreadsheet    $spreadsheet
32
     * @param    boolean        $includeCharts    Flag indicating if we should include drawing details for charts
33
     * @return string                  XML Output
34
     * @throws     \PhpSpreadsheet\Writer\Exception
35
     */
36
    public function writeContentTypes(\PhpSpreadsheet\Spreadsheet $spreadsheet = null, $includeCharts = false)
37
    {
38
        // Create XML writer
39
        $objWriter = null;
0 ignored issues
show
Unused Code introduced by
$objWriter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40 View Code Duplication
        if ($this->getParentWriter()->getUseDiskCaching()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getUseDiskCaching() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\BaseWriter, PhpSpreadsheet\Writer\CSV, PhpSpreadsheet\Writer\Excel2007, PhpSpreadsheet\Writer\Excel5, PhpSpreadsheet\Writer\OpenDocument.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
41
            $objWriter = new \PhpSpreadsheet\Shared\XMLWriter(\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDiskCachingDirectory() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\BaseWriter, PhpSpreadsheet\Writer\CSV, PhpSpreadsheet\Writer\Excel2007, PhpSpreadsheet\Writer\Excel5, PhpSpreadsheet\Writer\OpenDocument.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
42
        } else {
43
            $objWriter = new \PhpSpreadsheet\Shared\XMLWriter(\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY);
44
        }
45
46
        // XML header
47
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
48
49
        // Types
50
        $objWriter->startElement('Types');
51
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
52
53
        // Theme
54
        $this->writeOverrideContentType($objWriter, '/xl/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml');
55
56
        // Styles
57
        $this->writeOverrideContentType($objWriter, '/xl/styles.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml');
58
59
        // Rels
60
        $this->writeDefaultContentType($objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml');
61
62
        // XML
63
        $this->writeDefaultContentType($objWriter, 'xml', 'application/xml');
64
65
        // VML
66
        $this->writeDefaultContentType($objWriter, 'vml', 'application/vnd.openxmlformats-officedocument.vmlDrawing');
67
68
        // Workbook
69
        if ($spreadsheet->hasMacros()) { //Macros in workbook ?
70
            // Yes : not standard content but "macroEnabled"
71
            $this->writeOverrideContentType($objWriter, '/xl/workbook.xml', 'application/vnd.ms-excel.sheet.macroEnabled.main+xml');
72
            //... and define a new type for the VBA project
73
            $this->writeDefaultContentType($objWriter, 'bin', 'application/vnd.ms-office.vbaProject');
74
            if ($spreadsheet->hasMacrosCertificate()) {// signed macros ?
0 ignored issues
show
Bug introduced by
It seems like $spreadsheet is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
75
                // Yes : add needed information
76
                $this->writeOverrideContentType($objWriter, '/xl/vbaProjectSignature.bin', 'application/vnd.ms-office.vbaProjectSignature');
77
            }
78
        } else {// no macros in workbook, so standard type
79
            $this->writeOverrideContentType($objWriter, '/xl/workbook.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml');
80
        }
81
82
        // DocProps
83
        $this->writeOverrideContentType($objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml');
84
85
        $this->writeOverrideContentType($objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml');
86
87
        $customPropertyList = $spreadsheet->getProperties()->getCustomProperties();
88
        if (!empty($customPropertyList)) {
89
            $this->writeOverrideContentType($objWriter, '/docProps/custom.xml', 'application/vnd.openxmlformats-officedocument.custom-properties+xml');
90
        }
91
92
        // Worksheets
93
        $sheetCount = $spreadsheet->getSheetCount();
94
        for ($i = 0; $i < $sheetCount; ++$i) {
95
            $this->writeOverrideContentType($objWriter, '/xl/worksheets/sheet' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml');
96
        }
97
98
        // Shared strings
99
        $this->writeOverrideContentType($objWriter, '/xl/sharedStrings.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml');
100
101
        // Add worksheet relationship content types
102
        $chart = 1;
103
        for ($i = 0; $i < $sheetCount; ++$i) {
104
            $drawings = $spreadsheet->getSheet($i)->getDrawingCollection();
105
            $drawingCount = count($drawings);
106
            $chartCount = ($includeCharts) ? $spreadsheet->getSheet($i)->getChartCount() : 0;
107
108
            //    We need a drawing relationship for the worksheet if we have either drawings or charts
109
            if (($drawingCount > 0) || ($chartCount > 0)) {
110
                $this->writeOverrideContentType($objWriter, '/xl/drawings/drawing' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.drawing+xml');
111
            }
112
113
            //    If we have charts, then we need a chart relationship for every individual chart
114
            if ($chartCount > 0) {
115
                for ($c = 0; $c < $chartCount; ++$c) {
116
                    $this->writeOverrideContentType($objWriter, '/xl/charts/chart' . $chart++ . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml');
117
                }
118
            }
119
        }
120
121
        // Comments
122
        for ($i = 0; $i < $sheetCount; ++$i) {
123
            if (count($spreadsheet->getSheet($i)->getComments()) > 0) {
124
                $this->writeOverrideContentType($objWriter, '/xl/comments' . ($i + 1) . '.xml', 'application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml');
125
            }
126
        }
127
128
        // Add media content-types
129
        $aMediaContentTypes = array();
130
        $mediaCount = $this->getParentWriter()->getDrawingHashTable()->count();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDrawingHashTable() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\Excel2007.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
131
        for ($i = 0; $i < $mediaCount; ++$i) {
132
            $extension     = '';
133
            $mimeType     = '';
134
135
            if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof \PhpSpreadsheet\Worksheet\Drawing) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDrawingHashTable() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\Excel2007.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
136
                $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getExtension());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDrawingHashTable() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\Excel2007.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
137
                $mimeType = $this->getImageMimeType($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getPath());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDrawingHashTable() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\Excel2007.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
138
            } elseif ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof \PhpSpreadsheet\Worksheet\MemoryDrawing) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDrawingHashTable() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\Excel2007.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
139
                $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDrawingHashTable() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\Excel2007.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
140
                $extension = explode('/', $extension);
141
                $extension = $extension[1];
142
143
                $mimeType = $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface PhpSpreadsheet\Writer\IWriter as the method getDrawingHashTable() does only exist in the following implementations of said interface: PhpSpreadsheet\Writer\Excel2007.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
144
            }
145
146
            if (!isset($aMediaContentTypes[$extension])) {
147
                $aMediaContentTypes[$extension] = $mimeType;
148
149
                $this->writeDefaultContentType($objWriter, $extension, $mimeType);
150
            }
151
        }
152
        if ($spreadsheet->hasRibbonBinObjects()) {
153
            // Some additional objects in the ribbon ?
154
            // we need to write "Extension" but not already write for media content
155
            $tabRibbonTypes=array_diff($spreadsheet->getRibbonBinObjects('types'), array_keys($aMediaContentTypes));
156
            foreach ($tabRibbonTypes as $aRibbonType) {
157
                $mimeType='image/.'.$aRibbonType;//we wrote $mimeType like customUI Editor
158
                $this->writeDefaultContentType($objWriter, $aRibbonType, $mimeType);
159
            }
160
        }
161
        $sheetCount = $spreadsheet->getSheetCount();
162
        for ($i = 0; $i < $sheetCount; ++$i) {
163
            if (count($spreadsheet->getSheet()->getHeaderFooter()->getImages()) > 0) {
164
                foreach ($spreadsheet->getSheet()->getHeaderFooter()->getImages() as $image) {
165
                    if (!isset($aMediaContentTypes[strtolower($image->getExtension())])) {
166
                        $aMediaContentTypes[strtolower($image->getExtension())] = $this->getImageMimeType($image->getPath());
167
168
                        $this->writeDefaultContentType($objWriter, strtolower($image->getExtension()), $aMediaContentTypes[strtolower($image->getExtension())]);
169
                    }
170
                }
171
            }
172
        }
173
174
        $objWriter->endElement();
175
176
        // Return
177
        return $objWriter->getData();
178
    }
179
180
    /**
181
     * Get image mime type
182
     *
183
     * @param     string    $pFile    Filename
184
     * @return     string    Mime Type
185
     * @throws     \PhpSpreadsheet\Writer\Exception
186
     */
187
    private function getImageMimeType($pFile = '')
188
    {
189
        if (\PhpSpreadsheet\Shared\File::fileExists($pFile)) {
190
            $image = getimagesize($pFile);
191
            return image_type_to_mime_type($image[2]);
192
        } else {
193
            throw new \PhpSpreadsheet\Writer\Exception("File $pFile does not exist");
194
        }
195
    }
196
197
    /**
198
     * Write Default content type
199
     *
200
     * @param     \PhpSpreadsheet\Shared\XMLWriter     $objWriter         XML Writer
201
     * @param     string                         $pPartname         Part name
202
     * @param     string                         $pContentType     Content type
203
     * @throws     \PhpSpreadsheet\Writer\Exception
204
     */
205 View Code Duplication
    private function writeDefaultContentType(\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
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...
206
    {
207
        if ($pPartname != '' && $pContentType != '') {
208
            // Write content type
209
            $objWriter->startElement('Default');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
210
            $objWriter->writeAttribute('Extension', $pPartname);
211
            $objWriter->writeAttribute('ContentType', $pContentType);
212
            $objWriter->endElement();
213
        } else {
214
            throw new \PhpSpreadsheet\Writer\Exception("Invalid parameters passed.");
215
        }
216
    }
217
218
    /**
219
     * Write Override content type
220
     *
221
     * @param     \PhpSpreadsheet\Shared\XMLWriter     $objWriter         XML Writer
222
     * @param     string                         $pPartname         Part name
223
     * @param     string                         $pContentType     Content type
224
     * @throws     \PhpSpreadsheet\Writer\Exception
225
     */
226 View Code Duplication
    private function writeOverrideContentType(\PhpSpreadsheet\Shared\XMLWriter $objWriter = null, $pPartname = '', $pContentType = '')
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...
227
    {
228
        if ($pPartname != '' && $pContentType != '') {
229
            // Write content type
230
            $objWriter->startElement('Override');
0 ignored issues
show
Bug introduced by
It seems like $objWriter is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
231
            $objWriter->writeAttribute('PartName', $pPartname);
232
            $objWriter->writeAttribute('ContentType', $pContentType);
233
            $objWriter->endElement();
234
        } else {
235
            throw new \PhpSpreadsheet\Writer\Exception("Invalid parameters passed.");
236
        }
237
    }
238
}
239