Completed
Push — master ( 176090...b969a1 )
by Peter
02:10
created

Writer::write()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 28
cts 28
cp 1
rs 8.4032
c 0
b 0
f 0
cc 6
nc 3
nop 2
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace Maslosoft\Addendum\Cache\PhpCache;
5
6
7
use function array_filter;
8
use function array_sum;
9
use function chmod;
10
use function file_exists;
11
use function file_put_contents;
12
use Maslosoft\Addendum\Helpers\Cacher;
13
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
14
use Maslosoft\Addendum\Utilities\ClassChecker;
15
use Maslosoft\Addendum\Utilities\NameNormalizer;
16
use Maslosoft\Cli\Shared\Helpers\PhpExporter;
17
use Maslosoft\Cli\Shared\Io;
18
use function sprintf;
19
20
class Writer extends CacheComponent
21
{
22
23 35
	public function write($className, $data): bool
24
	{
25 35
		NameNormalizer::normalize($className, false);
26
27
28
		// Set partials cache
29
		$filter = function ($partial) use ($className) {
30 34
			$interface = AnnotatedInterface::class;
31 34
			NameNormalizer::normalize($interface, false);
32 34
			NameNormalizer::normalize($partial, false);
33 34
			if ($partial === $interface)
34
			{
35 34
				return false;
36
			}
37 34
			if ($partial === $className)
38
			{
39 34
				return false;
40
			}
41 12
			return true;
42 35
		};
43 35
		$partials = array_filter(ClassChecker::getPartials($className), $filter);
44 35
		$success = [];
45 35
		if (!empty($partials))
46
		{
47
			// Create directory for current class
48
			// This directory will hold class names
49
			// of partials
50 12
			$partialsDir = $this->getPartialsDir($className);
51 12
			if (!file_exists($partialsDir))
52
			{
53 12
				Io::mkdir($partialsDir);
54
			}
55
56 12
			foreach ($partials as $partialClass)
57
			{
58 12
				$partialFile = Cacher::classToFile($partialClass);
59
60
				// Write current class name to partial dir
61
				// so that it can be retrieved on modification
62
				// time check
63 12
				$classMarkerFile = sprintf(
64 12
					'%s/%s.php',
65 12
					$partialsDir,
66 12
					$partialFile
67
				);
68 12
				$success[] = (bool)file_put_contents($classMarkerFile, PhpExporter::export(true));
69
			}
70
		}
71 35
		$fileName = $this->getFilename($className);
72 35
		$success[] = (bool)file_put_contents($fileName, PhpExporter::export($data));
73 33
		@chmod($fileName, 0666);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
74 33
		return array_sum($success) === count($success);
75
	}
76
}