FileList   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A manipulateEditIcons() 0 29 3
A getFileMetaUidByCells() 0 11 3
1
<?php
2
3
/**
4
 * Extends the file list.
5
 */
6
7
namespace HDNET\Focuspoint\Hooks;
8
9
use HDNET\Autoloader\Annotation\Hook;
10
use HDNET\Focuspoint\Service\WizardService;
11
use HDNET\Focuspoint\Utility\FileUtility;
12
use HDNET\Focuspoint\Utility\ImageUtility;
13
use TYPO3\CMS\Backend\Utility\BackendUtility;
14
use TYPO3\CMS\Core\Resource\FileInterface;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Filelist\FileListEditIconHookInterface;
17
18
/**
19
 * Extends the file list.
20
 *
21
 * @Hook("TYPO3_CONF_VARS|SC_OPTIONS|fileList|editIconsHook")
22
 */
23
class FileList implements FileListEditIconHookInterface
24
{
25
    /**
26
     * Modifies edit icon array.
27
     *
28
     * @param array                        $cells        Array of edit icons
29
     * @param \TYPO3\CMS\Filelist\FileList $parentObject Parent object
30
     */
31
    public function manipulateEditIcons(&$cells, &$parentObject)
32
    {
33
        /** @var WizardService $wizardService */
34
        $wizardService = GeneralUtility::makeInstance(WizardService::class);
35
36
        try {
37
            $metaUid = $this->getFileMetaUidByCells($cells);
38
            $file = FileUtility::getFileByMetaData($metaUid);
39
        } catch (\Exception $ex) {
40
            $cells['focuspoint'] = $wizardService->getWizardButton();
41
42
            return;
43
        }
44
45
        if (!ImageUtility::isValidFileExtension($file->getExtension())) {
46
            $cells['focuspoint'] = $wizardService->getWizardButton();
47
48
            return;
49
        }
50
51
        $wizardArguments = [
52
            'P' => [
53
                'metaUid' => $metaUid,
54
                'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
55
            ],
56
        ];
57
        $wizardUri = BackendUtility::getModuleUrl('focuspoint', $wizardArguments);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Backend\Utilit...Utility::getModuleUrl() has been deprecated with message: since TYPO3 v9, will be removed in TYPO3 v10.0. Use UriBuilder instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
58
        $cells['focuspoint'] = $wizardService->getWizardButton($wizardUri);
59
    }
60
61
    /**
62
     * Get the file object of the given cell information.
63
     *
64
     * @param array $cells
65
     *
66
     * @throws \Exception
67
     *
68
     * @return int
69
     */
70
    protected function getFileMetaUidByCells($cells)
71
    {
72
        if ($cells['__fileOrFolderObject'] instanceof FileInterface) {
73
            $metaData = $cells['__fileOrFolderObject']->_getMetaData();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface TYPO3\CMS\Core\Resource\FileInterface as the method _getMetaData() does only exist in the following implementations of said interface: TYPO3\CMS\Core\Resource\File.

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...
74
        }
75
        if (!isset($metaData['uid'])) {
76
            throw new \Exception('No meta data found', 1475144024);
77
        }
78
79
        return (int) $metaData['uid'];
80
    }
81
}
82