Completed
Push — master ( a4d031...ae8bc9 )
by Roman
02:16
created

src/RM/SMSender/DI/SMSenderExtension.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace RM\SMSender\DI;
4
5
use Nette\Reflection\ClassType;
6
use Nette\Configurator;
7
use Nette\DI\Compiler;
8
use Nette\DI\CompilerExtension;
9
use Nette\DI\Helpers;
10
11
/**
12
 * Nette DI extension for SMSender.
13
 */
14 1
class SMSenderExtension extends CompilerExtension
15
{
16
	/** @var array */
17
	public $defaults = [
18
		'config' => [],
19
		'senderClass' => 'RM\SMSender\EuroSms\Sender',
20
		'setDebugMode' => FALSE,
21
		'messageClass' => 'RM\SMSender\EuroSms\Message',
22
		'messageFactoryClass' => 'RM\SMSender\MessageFactory',
23
		'message' => [
24
			'setFrom' => NULL,
25
			'signature' => NULL,
26
		],
27
	];
28
29
	public function beforeCompile()
30
	{
31 1
		$builder = $this->getContainerBuilder();
32
33 1
		$config = $this->validateConfig($this->defaults);
34
35 1
		$sender = $builder->addDefinition($this->prefix('sender'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated with message: Use setType() 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...
36 1
			->setClass($config['senderClass']);
37
38 1
		foreach ($config as $method => $value) {
0 ignored issues
show
The expression $config of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
39 1
			$tmp = new $config['senderClass'];
40 1
			if ((new ClassType($tmp))->hasMethod($method)) {
41 1
				$sender->addSetup($method, [$value]);
42
			}
43
		}
44
45 1
		if ($config['message']['signature']) {
46 1
			$sender->addSetup('$service->onBeforeSend[] = function ($message) { $message->setText($message->getText() . ?); };', [$config['message']['signature']]);
47
		}
48
49 1
		$builder->addDefinition($this->prefix('messageFactory'))
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ServiceDefinition::setClass() has been deprecated with message: Use setType() 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...
50 1
			->setClass($config['messageFactoryClass'])
51 1
			->addSetup('$class', [$config['messageClass']])
52 1
			->addSetup('$params', [$config['message']]);
53 1
	}
54
55
	/**
56
	 * Register extension to DI Container.
57
	 * @param  Configurator $config
58
	 */
59
	public static function register(Configurator $config)
60
	{
61 1
		$config->onCompile[] = function (Configurator $config, Compiler $compiler) {
62 1
			$compiler->addExtension('smsender', new SMSenderExtension());
63 1
		};
64 1
	}
65
}
66