GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#138)
by Jason
10:32
created

BlockUpgradeTask::run()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 57
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.7433
c 0
b 0
f 0
cc 6
eloc 21
nc 6
nop 1

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
namespace SheaDawson\Blocks\Tasks;
4
5
use SheaDawson\Blocks\Model\Block;
6
use SheaDawson\Blocks\Model\BlockSet;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\SiteConfig\SiteConfig;
9
use SilverStripe\Dev\BuildTask;
10
11
/**
12
 * BlockUpgradeTask
13
 * Run this task to migrate from Blocks 0.x to 1.x.
14
 *
15
 * @author Shea Dawson <[email protected]>
16
 */
17
class BlockUpgradeTask extends BuildTask
18
{
19
	public function run($request)
20
	{
21
22
		// update block/set titles
23
		// Name field has been reverted back to Title
24
		// DB::query("update Block set Name = Title");
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
		// DB::query("update BlockSet set Name = Title");
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
27
		// update block areas
28
29
		DB::query('
30
			update SiteTree_Blocks
31
			left join Block on SiteTree_Blocks.BlockID = Block.ID
32
			set BlockArea = Block.Area
33
			where BlockID = Block.ID
34
		');
35
36
		// update block sort
37
38
		DB::query('
39
			update SiteTree_Blocks
40
			left join Block on SiteTree_Blocks.BlockID = Block.ID
41
			set Sort = Block.Weight
42
			where BlockID = Block.ID
43
		');
44
45
		echo 'BlockAreas, Sort updated<br />';
46
47
		// migrate global blocks
48
49
		$sc = SiteConfig::current_site_config();
50
		if ($sc->Blocks()->Count()) {
51
			$set = BlockSet::get()->filter('Title', 'Global')->first();
52
			if (!$set) {
53
				$set = BlockSet::create([
54
					'Title' => 'Global',
55
				]);
56
				$set->write();
57
			}
58
			foreach ($sc->Blocks() as $block) {
59
				if (!$set->Blocks()->find('ID', $block->ID)) {
60
					$set->Blocks()->add($block, [
61
						'Sort' => $block->Weight,
62
						'BlockArea' => $block->Area,
63
					]);
64
					echo "Block #$block->ID added to Global block set<br />";
65
				}
66
			}
67
		}
68
69
		// publish blocks
70
		$blocks = Block::get()->filter('Published', 1);
71
		foreach ($blocks as $block) {
72
			$block->publish('Stage', 'Live');
73
			echo "Published Block #$block->ID<br />";
74
		}
75
	}
76
}
77