Completed
Push — master ( acb674...7279e3 )
by Yuichi
06:47
created

File   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 69
ccs 23
cts 23
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 7 1
A post() 0 17 1
A getFilename() 0 8 2
1
<?php
2
3
namespace CybozuHttp\Api\Kintone;
4
5
use CybozuHttp\Client;
6
use CybozuHttp\Api\KintoneApi;
7
8
9
/**
10
 * @author ochi51 <[email protected]>
11
 */
12
class File
13
{
14
    /**
15
     * @var Client
16
     */
17
    private $client;
18
19 1
    public function __construct(Client $client)
20
    {
21 1
        $this->client = $client;
22 1
    }
23
24
    /**
25
     * Get file
26
     * https://cybozudev.zendesk.com/hc/ja/articles/202166180#step1
27
     *
28
     * @param string $fileKey
29
     * @param int $guestSpaceId
30
     * @return string
31
     */
32 1
    public function get($fileKey, $guestSpaceId = null)
33
    {
34 1
        $options = ['json' => ['fileKey' => $fileKey]];
35 1
        $response = $this->client->get(KintoneApi::generateUrl('file.json', $guestSpaceId), $options);
36
37 1
        return (string)$response->getBody();
38
    }
39
40
    /**
41
     * Post file
42
     * https://cybozudev.zendesk.com/hc/ja/articles/201941824#step1
43
     *
44
     * @param string $filename
45
     * @param int $guestSpaceId
46
     * @return string
47
     */
48 1
    public function post($filename, $guestSpaceId = null)
49
    {
50
        $options = ['multipart' =>  [
51
            [
52 1
                'name' => 'file',
53 1
                'filename' => self::getFilename($filename),
54 1
                'contents' => fopen($filename, 'rb'),
55
                'headers' => [
56 1
                    'Content-Type' => mime_content_type($filename)
57 1
                ]
58 1
            ]
59 1
        ]];
60
61 1
        return $this->client
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\StreamInterface as the method jsonSerialize() does only exist in the following implementations of said interface: CybozuHttp\Middleware\JsonStream.

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...
62 1
            ->post(KintoneApi::generateUrl('file.json', $guestSpaceId), $options)
63 1
            ->getBody()->jsonSerialize()['fileKey'];
64
    }
65
66
    /**
67
     * Returns locale independent base name of the given path.
68
     *
69
     * @param string $name The new file name
70
     * @return string containing
71
     */
72 10
    public static function getFilename($name)
73
    {
74 10
        $originalName = str_replace('\\', '/', $name);
75 10
        $pos = strrpos($originalName, '/');
76 10
        $originalName = false === $pos ? $originalName : substr($originalName, $pos + 1);
77
78 10
        return $originalName;
79
    }
80
}