Search and Find

Book Title

Author/Publisher

Table of Contents

Show eBooks for my device only:

 

Pro PHP Refactoring

Pro PHP Refactoring

of: Francesco Trucchia, Jacopo Romei

Apress, 2011

ISBN: 9781430227281 , 360 Pages

Format: PDF, Read online

Copy protection: DRM

Windows PC,Mac OSX,Windows PC,Mac OSX geeignet für alle DRM-fähigen eReader Apple iPad, Android Tablet PC's Read Online for: Windows PC,Mac OSX,Linux

Price: 52,99 EUR



More of the content

Pro PHP Refactoring


 

Title Page

Title Page

Copyright Page

Copyright Page

Contents at a Glance

3

Table of Contents

Table of Contents

About the Authors

18

About the Technical Reviewer

19

Acknowledgments

20

Introduction

21

CHAPTER 1 Introduction

CHAPTER 1 Introduction

Lesson Learned

23

Hidden Gems

23

You Don’t Know What You’ve Got ’til It’s Gone

23

Call of Duty

24

CHAPTER 2 Finding “Bad Smells” in Code

CHAPTER 2 Finding “Bad Smells” in Code

Why Code Can Smell

25

Duplicated Code

26

Long Method

27

Large Class

30

Long Parameter List

32

Divergent Change

33

Shotgun Surgery

35

Feature Envy

35

Data Clamps

36

Primitive Obsession

37

Switch Statements

38

Lazy Class

39

Speculative Generality

40

Temporary Field

40

Data Class

41

Comments

42

Procedural Code

44

CHAPTER 3 Introduction to Refactoring

CHAPTER 3 Introduction to Refactoring

The Concept: What Refactoring Is

45

The Reason: What’s the Goal of Refactoring?

45

Architecture and Structure, They Fade Away

46

Reworking Chaos into Well-Designed Code

46

An Example, at Last

47

Look Ma’! No Comments!

51

Once Is Better than Twice

53

Goliath Died in the End

55

CHAPTER 4 Principles and Rules

CHAPTER 4 Principles and Rules

Why Should You Do Refactoring?

67

Refactoring Improves the Design of Our Software

67

Refactoring Makes Software Easier to Understand

68

Keep It Simple and Stupid (KISS)

68

Don’t Repeat Yourself

69

Test-Driven Development (TDD)

69

Refactoring Helps You Find Bugs

69

Refactoring Increases Our Productivity

70

When Should We Do Refactoring?

71

The Rule of Three

71

Refactoring When You Add Functionality

72

Refactoring When You Need to Fix a Bug

72

When You Shouldn’t Do Refactoring

73

Some Simple Rules

73

Test Before Refactoring

73

Small and Simple Changes

74

Never Change Functionality

74

Follow the Bad Smells

74

Follow Refactoring Techniques Step-by-Step

75

Summary

75

CHAPTER 5 Test-First Development

CHAPTER 5 Test-First Development

Building Value One-Way

76

Chaos in a Cage

77

Unit Tests

77

Functional Tests

77

You Don't Know What You’ve Got ’til It’s Gone

78

Trust Me: Communication

78

Listen to What You Say, and Write It Down

79

Pleasant Constraints

79

Create Trust

80

Test-Driven Development

80

Summary

81

CHAPTER 6 Refactoring Tools

CHAPTER 6 Refactoring Tools

PHP IDE

82

Refactoring Activities

82

Rename

82

Move

83

Encapsulate Field

83

Override

83

Safely Remove

83

Cross-Platform Open-Source IDE

83

Unit Tests with PHPUnit

83

What Is It?

83

Installation

84

PEAR Installation

84

Manual Installation

84

How to Write Unit Tests

85

How to Run Tests

89

How to Organize Our Tests

90

Test Doubles

93

Stub Object

94

Mock Object

95

PHPUnit Conclusion

96

Functional Test with Selenium

96

What Is It?

96

Selenium IDE

97

Selenium RC

97

Selenium Grid

97

Installation

97

How to Record and Run Functional Tests

98

How to Organize Selenium Tests

100

Automated Test Execution with Selenium RC

101

Selenium Conclusion

101

The Best of Two Worlds

101

Selenium RC and PHPUnit

101

Selenium Functional Test with PHPUnit

101

Summary

103

CHAPTER 7 Structuring Behavior

CHAPTER 7 Structuring Behavior

Extract Method

104

Motivation

104

Mechanics

105

Example: No Local Variables

105

Example: Using Local Variables

107

Example: Reassigning a Local Variable

107

Inline Method

111

Motivation

111

Mechanics

111

Example

112

Inline Temp

113

Motivation

113

Mechanics

113

Example

113

Replace Temp with Query

114

Motivation

114

Mechanics

114

Example

115

Introduce Explaining Variable

117

Motivation

117

Mechanics

117

Example

117

Split Temporary Variable

118

Motivation

118

Mechanics

119

Example

119

Replace Method with Method Object

121

Motivation

121

Mechanics

121

Example

121

Substitute Algorithm

123

Motivation

123

Mechanics

124

Example

124

Summary

125

CHAPTER 8 Changing Class Responsibilities

CHAPTER 8 Changing Class Responsibilities

Move Method

126

Motivation

126

Mechanics

127

Example

127

Move Property (or Field)

131

Motivation

131

Mechanics

131

Example

132

Extract Class

134

Motivation

134

Mechanics

134

Example

135

Inline Class

140

Motivation

140

Mechanics

140

Example

140

Hide Delegate

143

Motivation

143

Mechanics

143

Example

143

Remove the Middle Man

146

Motivation

146

Mechanism

146

Example

147

Introduce Foreign Method

149

Motivation

149

Mechanism

150

Example

150

Summary

151

CHAPTER 9 Dealing with Data Rationalization

CHAPTER 9 Dealing with Data Rationalization

Self-Encapsulate Field

152

Motivation

152

Mechanics

152

Example

153

Replace Data Value with Object

154

Motivation

154

Mechanics

154

Example

154

Change Value to Reference

156

Motivation

156

Mechanics

156

Example

157

Change Reference to Value

159

Motivation

159

Mechanics

159

Replace Array with Object

159

Motivation

160

Mechanics

160

Example

160

Change Unidirectional Association to Bidirectional

162

Motivation

162

Mechanics

162

Example

163

Change Bidirectional Association to Unidirectional

165

Motivation

165

Mechanics

165

Example

166

Replace Magic Number with Symbolic Constant

168

Motivation

168

Mechanics

168

Example

168

Encapsulate Field

169

Motivation

169

Mechanics

170

Example

170

Replacing Type Code with Subclasses

171

Motivation

171

Mechanics

171

Example

171

Replace Type Code with State/Strategy

175

Motivation

175

Mechanics

175

Example

175

Replace Subclass with Fields

180

Motivation

180

Mechanics

180

Example

181

Summary

185

CHAPTER 10 Reducing to Essential Conditional Executions

CHAPTER 10 Reducing to Essential Conditional Executions

Decompose Conditional

186

Motivation

186

Mechanics

187

Example

187

Consolidate Conditional Expression

189

Motivation

189

Mechanism

190

Example to Consolidate with ORs

190

Example to Consolidate with AND

191

Consolidate Duplicate Conditional Fragments

192

Motivation

192

Mechanism

192

Example

193

Remove Control Flag

194

Motivation

194

Mechanism

194

Example with Control Flag Replaced with Break

195

Example Replacing Control Flag with a Return Exit Point

196

Replace Nested Conditional with Guard Clauses

198

Motivation

198

Mechanism

198

Example

199

Replace Conditional with Polymorphism

201

Motivation

201

Mechanism

202

Example

202

Summary

205

CHAPTER 11 Simplifying Method Calls

CHAPTER 11 Simplifying Method Calls

Rename Method

206

Motivation

206

Mechanics

206

Example

207

Add Parameter

208

Motivation

208

Mechanics

208

Example

209

Remove Parameter

210

Motivation

210

Mechanics

210

Separate Query from Modifier

211

Motivation

211

Mechanics

211

Example

211

Parameterize Method

213

Motivation

213

Mechanics

214

Example

214

Replace Parameter with Explicit Method

215

Motivation

215

Mechanics

216

Example

216

Preserve Whole Object

218

Motivation

218

Mechanics

218

Example

219

Replace Parameter with Method

220

Motivation

221

Mechanics

221

Example

221

Introduce Parameter Object

222

Motivation

222

Mechanics

223

Remove Setting Method

223

Motivation

223

Mechanics

223

Example

223

Hide Method

225

Motivation

225

Mechanics

225

Replace Constructor with Factory Method

225

Motivation

225

Mechanics

226

Example

226

Replace Error Code with Exception

227

Motivation

227

Mechanics

227

Example

227

Replace Exception with Test

229

Motivation

229

Mechanics

229

Summary

229

CHAPTER 12 Simplifying Generalization Relationships

CHAPTER 12 Simplifying Generalization Relationships

Pull Up Field

230

Motivation

230

Mechanism

231

Example

231

Pull Up Method

232

Motivation

232

Mechanism

233

Example

233

Pull Up Constructor Body

235

Motivation

235

Mechanism

236

Example

236

Push Down Method

239

Motivation

239

Mechanism

239

Example

239

Push Down Field

242

Motivation

242

Mechanism

242

Example

242

Extract Subclass

245

Motivation

245

Mechanism

245

Example

246

Extract Super Class

250

Motivation

250

Mechanism

250

Example

251

Collapse Hierarchy

254

Motivation

254

Mechanism

254

Example

255

Form Template Method

257

Motivation

257

Mechanism

257

Example

258

Replace Inheritance with Delegation

262

Motivation

262

Mechanism

263

Example

263

Replace Delegation with Inheritance

266

Motivation

266

Mechanism

266

Example

267

Summary

268

CHAPTER 13 Legacy Code

CHAPTER 13 Legacy Code

Ugly Code

269

index.php

272

config.php

273

functions.php

273

header.php

274

footer.php

275

new.php

275

edit.php

276

_form.php

278

remove.php

278

Maintenance

279

Example: SQL injection

279

Example: Database Portability

280

New Features

280

Dynamic Layouts

280

Internationalization

281

Break the Cycle

281

Summary

282

CHAPTER 14 Regression Tests

CHAPTER 14 Regression Tests

Ugly But Valuable

283

Keeping Value vs. Wasting Value

284

Putting the Chaos in a Cage

284

Motivation

284

Mechanics

285

Examples

285

Add a New Record

286

Edit a Record

288

Read a List of Records

289

Remove a Record

290

Validate a Record

291

Test Refactoring

292

Unify Test Cases in a Suite

293

Summary

294

CHAPTER 15 Refactoring with Patterns

CHAPTER 15 Refactoring with Patterns

Design Patterns

295

What Are Design Patterns?

295

Why Do I Need to Use Design Patterns?

296

When Do I Need to Use Them?

296

Refactoring with Patterns

296

Transform Procedural Code into Object-Oriented Code

297

Motivation

297

Mechanism

298

Example

299

Working with Fixtures

299

Index Action

300

Add Action

304

Edit Action

309

Remove Action

314

Replace SQL with ORM

316

Motivation

316

Mechanism

317

Example

317

Separate Business Logic from View

326

Motivation

326

Mechanism

326

Example

327

Decorator Design Pattern

327

Template View Pattern

333

MVC Architecture

337

Motivation

337

Mechanism

337

Example

338

Summary

345

Index

346